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_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;
344 sin6p = (struct sockaddr_in6 *)nam;
345 if (nam->sa_len != sizeof (*sin6p))
348 * Must check for multicast addresses and disallow binding
351 if (sin6p->sin6_family == AF_INET6 &&
352 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
353 return (EAFNOSUPPORT);
357 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
359 vflagsav = inp->inp_vflag;
360 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
366 INP_HASH_WLOCK(&V_tcbinfo);
367 inp->inp_vflag &= ~INP_IPV4;
368 inp->inp_vflag |= INP_IPV6;
370 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
371 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
372 inp->inp_vflag |= INP_IPV4;
373 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
374 struct sockaddr_in sin;
376 in6_sin6_2_sin(&sin, sin6p);
377 inp->inp_vflag |= INP_IPV4;
378 inp->inp_vflag &= ~INP_IPV6;
379 error = in_pcbbind(inp, (struct sockaddr *)&sin,
381 INP_HASH_WUNLOCK(&V_tcbinfo);
386 error = in6_pcbbind(inp, nam, td->td_ucred);
387 INP_HASH_WUNLOCK(&V_tcbinfo);
390 inp->inp_vflag = vflagsav;
392 TCP_PROBE2(debug__user, tp, PRU_BIND);
400 * Prepare to accept connections.
403 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
407 struct tcpcb *tp = NULL;
411 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
413 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
420 error = solisten_proto_check(so);
421 INP_HASH_WLOCK(&V_tcbinfo);
422 if (error == 0 && inp->inp_lport == 0)
423 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
424 INP_HASH_WUNLOCK(&V_tcbinfo);
426 tcp_state_change(tp, TCPS_LISTEN);
427 solisten_proto(so, backlog);
429 if ((so->so_options & SO_NO_OFFLOAD) == 0)
430 tcp_offload_listen_start(tp);
436 if (tp->t_flags & TF_FASTOPEN)
437 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
440 TCPDEBUG2(PRU_LISTEN);
441 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
449 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
453 struct tcpcb *tp = NULL;
458 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
460 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
464 vflagsav = inp->inp_vflag;
468 error = solisten_proto_check(so);
469 INP_HASH_WLOCK(&V_tcbinfo);
470 if (error == 0 && inp->inp_lport == 0) {
471 inp->inp_vflag &= ~INP_IPV4;
472 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
473 inp->inp_vflag |= INP_IPV4;
474 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
476 INP_HASH_WUNLOCK(&V_tcbinfo);
478 tcp_state_change(tp, TCPS_LISTEN);
479 solisten_proto(so, backlog);
481 if ((so->so_options & SO_NO_OFFLOAD) == 0)
482 tcp_offload_listen_start(tp);
488 if (tp->t_flags & TF_FASTOPEN)
489 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
492 inp->inp_vflag = vflagsav;
495 TCPDEBUG2(PRU_LISTEN);
496 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
504 * Initiate connection to peer.
505 * Create a template for use in transmissions on this connection.
506 * Enter SYN_SENT state, and mark socket as connecting.
507 * Start keep-alive timer, and seed output sequence space.
508 * Send initial segment on connection.
511 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
515 struct tcpcb *tp = NULL;
516 struct sockaddr_in *sinp;
518 sinp = (struct sockaddr_in *)nam;
519 if (nam->sa_len != sizeof (*sinp))
522 * Must disallow TCP ``connections'' to multicast addresses.
524 if (sinp->sin_family == AF_INET
525 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
526 return (EAFNOSUPPORT);
527 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
532 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
534 if (inp->inp_flags & INP_TIMEWAIT) {
538 if (inp->inp_flags & INP_DROPPED) {
539 error = ECONNREFUSED;
544 if ((error = tcp_connect(tp, nam, td)) != 0)
547 if (registered_toedevs > 0 &&
548 (so->so_options & SO_NO_OFFLOAD) == 0 &&
549 (error = tcp_offload_connect(so, nam)) == 0)
552 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
553 error = tp->t_fb->tfb_tcp_output(tp);
555 TCPDEBUG2(PRU_CONNECT);
556 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
564 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
568 struct tcpcb *tp = NULL;
569 struct sockaddr_in6 *sin6p;
575 sin6p = (struct sockaddr_in6 *)nam;
576 if (nam->sa_len != sizeof (*sin6p))
579 * Must disallow TCP ``connections'' to multicast addresses.
581 if (sin6p->sin6_family == AF_INET6
582 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
583 return (EAFNOSUPPORT);
586 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
588 vflagsav = inp->inp_vflag;
589 incflagsav = inp->inp_inc.inc_flags;
590 if (inp->inp_flags & INP_TIMEWAIT) {
594 if (inp->inp_flags & INP_DROPPED) {
595 error = ECONNREFUSED;
602 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
603 * therefore probably require the hash lock, which isn't held here.
604 * Is this a significant problem?
606 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
607 struct sockaddr_in sin;
609 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
613 if ((inp->inp_vflag & INP_IPV4) == 0) {
614 error = EAFNOSUPPORT;
618 in6_sin6_2_sin(&sin, sin6p);
619 if ((error = prison_remote_ip4(td->td_ucred,
620 &sin.sin_addr)) != 0)
622 inp->inp_vflag |= INP_IPV4;
623 inp->inp_vflag &= ~INP_IPV6;
624 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
627 if (registered_toedevs > 0 &&
628 (so->so_options & SO_NO_OFFLOAD) == 0 &&
629 (error = tcp_offload_connect(so, nam)) == 0)
632 error = tp->t_fb->tfb_tcp_output(tp);
635 if ((inp->inp_vflag & INP_IPV6) == 0) {
636 error = EAFNOSUPPORT;
641 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
643 inp->inp_vflag &= ~INP_IPV4;
644 inp->inp_vflag |= INP_IPV6;
645 inp->inp_inc.inc_flags |= INC_ISIPV6;
646 if ((error = tcp6_connect(tp, nam, td)) != 0)
649 if (registered_toedevs > 0 &&
650 (so->so_options & SO_NO_OFFLOAD) == 0 &&
651 (error = tcp_offload_connect(so, nam)) == 0)
654 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
655 error = tp->t_fb->tfb_tcp_output(tp);
659 * If the implicit bind in the connect call fails, restore
660 * the flags we modified.
662 if (error != 0 && inp->inp_lport == 0) {
663 inp->inp_vflag = vflagsav;
664 inp->inp_inc.inc_flags = incflagsav;
667 TCPDEBUG2(PRU_CONNECT);
668 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
675 * Initiate disconnect from peer.
676 * If connection never passed embryonic stage, just drop;
677 * else if don't need to let data drain, then can just drop anyways,
678 * else have to begin TCP shutdown process: mark socket disconnecting,
679 * drain unread data, state switch to reflect user close, and
680 * send segment (e.g. FIN) to peer. Socket will be really disconnected
681 * when peer sends FIN and acks ours.
683 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
686 tcp_usr_disconnect(struct socket *so)
689 struct tcpcb *tp = NULL;
693 INP_INFO_RLOCK(&V_tcbinfo);
695 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
697 if (inp->inp_flags & INP_TIMEWAIT)
699 if (inp->inp_flags & INP_DROPPED) {
707 TCPDEBUG2(PRU_DISCONNECT);
708 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
710 INP_INFO_RUNLOCK(&V_tcbinfo);
716 * Accept a connection. Essentially all the work is done at higher levels;
717 * just return the address of the peer, storing through addr.
720 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
723 struct inpcb *inp = NULL;
724 struct tcpcb *tp = NULL;
729 if (so->so_state & SS_ISDISCONNECTED)
730 return (ECONNABORTED);
733 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
735 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
736 error = ECONNABORTED;
743 * We inline in_getpeeraddr and COMMON_END here, so that we can
744 * copy the data of interest and defer the malloc until after we
747 port = inp->inp_fport;
748 addr = inp->inp_faddr;
751 TCPDEBUG2(PRU_ACCEPT);
752 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
755 *nam = in_sockaddr(port, &addr);
762 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
764 struct inpcb *inp = NULL;
766 struct tcpcb *tp = NULL;
768 struct in6_addr addr6;
773 if (so->so_state & SS_ISDISCONNECTED)
774 return (ECONNABORTED);
777 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
778 INP_INFO_RLOCK(&V_tcbinfo);
780 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
781 error = ECONNABORTED;
788 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
789 * copy the data of interest and defer the malloc until after we
792 if (inp->inp_vflag & INP_IPV4) {
794 port = inp->inp_fport;
795 addr = inp->inp_faddr;
797 port = inp->inp_fport;
798 addr6 = inp->in6p_faddr;
802 TCPDEBUG2(PRU_ACCEPT);
803 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
805 INP_INFO_RUNLOCK(&V_tcbinfo);
808 *nam = in6_v4mapsin6_sockaddr(port, &addr);
810 *nam = in6_sockaddr(port, &addr6);
817 * Mark the connection as being incapable of further output.
820 tcp_usr_shutdown(struct socket *so)
824 struct tcpcb *tp = NULL;
827 INP_INFO_RLOCK(&V_tcbinfo);
829 KASSERT(inp != NULL, ("inp == NULL"));
831 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
839 if (!(inp->inp_flags & INP_DROPPED))
840 error = tp->t_fb->tfb_tcp_output(tp);
843 TCPDEBUG2(PRU_SHUTDOWN);
844 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
846 INP_INFO_RUNLOCK(&V_tcbinfo);
852 * After a receive, possibly send window update to peer.
855 tcp_usr_rcvd(struct socket *so, int flags)
858 struct tcpcb *tp = NULL;
863 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
865 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
873 * For passively-created TFO connections, don't attempt a window
874 * update while still in SYN_RECEIVED as this may trigger an early
875 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
876 * application response data, or failing that, when the DELACK timer
879 if ((tp->t_flags & TF_FASTOPEN) &&
880 (tp->t_state == TCPS_SYN_RECEIVED))
884 if (tp->t_flags & TF_TOE)
885 tcp_offload_rcvd(tp);
888 tp->t_fb->tfb_tcp_output(tp);
892 TCP_PROBE2(debug__user, tp, PRU_RCVD);
898 * Do a send by putting data in output queue and updating urgent
899 * marker if URG set. Possibly send more data. Unlike the other
900 * pru_*() routines, the mbuf chains are our responsibility. We
901 * must either enqueue them or free them. The other pru_* routines
902 * generally are caller-frees.
905 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
906 struct sockaddr *nam, struct mbuf *control, struct thread *td)
910 struct tcpcb *tp = NULL;
917 * We require the pcbinfo lock if we will close the socket as part of
920 if (flags & PRUS_EOF)
921 INP_INFO_RLOCK(&V_tcbinfo);
923 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
925 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
929 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
930 * for freeing memory.
932 if (m && (flags & PRUS_NOTREADY) == 0)
938 isipv6 = nam && nam->sa_family == AF_INET6;
943 /* TCP doesn't do control messages (rights, creds, etc) */
944 if (control->m_len) {
951 m_freem(control); /* empty control, just free it */
953 if (!(flags & PRUS_OOB)) {
954 sbappendstream(&so->so_snd, m, flags);
955 if (nam && tp->t_state < TCPS_SYN_SENT) {
957 * Do implied connect if not yet connected,
958 * initialize window to default value, and
959 * initialize maxseg using peer's cached MSS.
963 error = tcp6_connect(tp, nam, td);
965 #if defined(INET6) && defined(INET)
969 error = tcp_connect(tp, nam, td);
973 tp->snd_wnd = TTCP_CLIENT_SND_WND;
976 if (flags & PRUS_EOF) {
978 * Close the send side of the connection after
981 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
985 if (!(inp->inp_flags & INP_DROPPED) &&
986 !(flags & PRUS_NOTREADY)) {
987 if (flags & PRUS_MORETOCOME)
988 tp->t_flags |= TF_MORETOCOME;
989 error = tp->t_fb->tfb_tcp_output(tp);
990 if (flags & PRUS_MORETOCOME)
991 tp->t_flags &= ~TF_MORETOCOME;
995 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
997 SOCKBUF_LOCK(&so->so_snd);
998 if (sbspace(&so->so_snd) < -512) {
999 SOCKBUF_UNLOCK(&so->so_snd);
1005 * According to RFC961 (Assigned Protocols),
1006 * the urgent pointer points to the last octet
1007 * of urgent data. We continue, however,
1008 * to consider it to indicate the first octet
1009 * of data past the urgent section.
1010 * Otherwise, snd_up should be one lower.
1012 sbappendstream_locked(&so->so_snd, m, flags);
1013 SOCKBUF_UNLOCK(&so->so_snd);
1014 if (nam && tp->t_state < TCPS_SYN_SENT) {
1016 * Do implied connect if not yet connected,
1017 * initialize window to default value, and
1018 * initialize maxseg using peer's cached MSS.
1022 error = tcp6_connect(tp, nam, td);
1024 #if defined(INET6) && defined(INET)
1028 error = tcp_connect(tp, nam, td);
1032 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1035 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1036 if (!(flags & PRUS_NOTREADY)) {
1037 tp->t_flags |= TF_FORCEDATA;
1038 error = tp->t_fb->tfb_tcp_output(tp);
1039 tp->t_flags &= ~TF_FORCEDATA;
1043 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1044 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1045 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1046 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1048 if (flags & PRUS_EOF)
1049 INP_INFO_RUNLOCK(&V_tcbinfo);
1054 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1060 inp = sotoinpcb(so);
1062 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1064 for (int i = 0; i < count; i++)
1066 return (ECONNRESET);
1068 tp = intotcpcb(inp);
1070 SOCKBUF_LOCK(&so->so_snd);
1071 error = sbready(&so->so_snd, m, count);
1072 SOCKBUF_UNLOCK(&so->so_snd);
1074 error = tp->t_fb->tfb_tcp_output(tp);
1081 * Abort the TCP. Drop the connection abruptly.
1084 tcp_usr_abort(struct socket *so)
1087 struct tcpcb *tp = NULL;
1090 inp = sotoinpcb(so);
1091 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1093 INP_INFO_RLOCK(&V_tcbinfo);
1095 KASSERT(inp->inp_socket != NULL,
1096 ("tcp_usr_abort: inp_socket == NULL"));
1099 * If we still have full TCP state, and we're not dropped, drop.
1101 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1102 !(inp->inp_flags & INP_DROPPED)) {
1103 tp = intotcpcb(inp);
1105 tp = tcp_drop(tp, ECONNABORTED);
1108 TCPDEBUG2(PRU_ABORT);
1109 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1111 if (!(inp->inp_flags & INP_DROPPED)) {
1113 so->so_state |= SS_PROTOREF;
1115 inp->inp_flags |= INP_SOCKREF;
1119 INP_INFO_RUNLOCK(&V_tcbinfo);
1123 * TCP socket is closed. Start friendly disconnect.
1126 tcp_usr_close(struct socket *so)
1129 struct tcpcb *tp = NULL;
1132 inp = sotoinpcb(so);
1133 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1135 INP_INFO_RLOCK(&V_tcbinfo);
1137 KASSERT(inp->inp_socket != NULL,
1138 ("tcp_usr_close: inp_socket == NULL"));
1141 * If we still have full TCP state, and we're not dropped, initiate
1144 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1145 !(inp->inp_flags & INP_DROPPED)) {
1146 tp = intotcpcb(inp);
1149 TCPDEBUG2(PRU_CLOSE);
1150 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1152 if (!(inp->inp_flags & INP_DROPPED)) {
1154 so->so_state |= SS_PROTOREF;
1156 inp->inp_flags |= INP_SOCKREF;
1159 INP_INFO_RUNLOCK(&V_tcbinfo);
1163 * Receive out-of-band data.
1166 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1170 struct tcpcb *tp = NULL;
1173 inp = sotoinpcb(so);
1174 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1176 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1180 tp = intotcpcb(inp);
1182 if ((so->so_oobmark == 0 &&
1183 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1184 so->so_options & SO_OOBINLINE ||
1185 tp->t_oobflags & TCPOOB_HADDATA) {
1189 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1190 error = EWOULDBLOCK;
1194 *mtod(m, caddr_t) = tp->t_iobc;
1195 if ((flags & MSG_PEEK) == 0)
1196 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1199 TCPDEBUG2(PRU_RCVOOB);
1200 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1206 struct pr_usrreqs tcp_usrreqs = {
1207 .pru_abort = tcp_usr_abort,
1208 .pru_accept = tcp_usr_accept,
1209 .pru_attach = tcp_usr_attach,
1210 .pru_bind = tcp_usr_bind,
1211 .pru_connect = tcp_usr_connect,
1212 .pru_control = in_control,
1213 .pru_detach = tcp_usr_detach,
1214 .pru_disconnect = tcp_usr_disconnect,
1215 .pru_listen = tcp_usr_listen,
1216 .pru_peeraddr = in_getpeeraddr,
1217 .pru_rcvd = tcp_usr_rcvd,
1218 .pru_rcvoob = tcp_usr_rcvoob,
1219 .pru_send = tcp_usr_send,
1220 .pru_ready = tcp_usr_ready,
1221 .pru_shutdown = tcp_usr_shutdown,
1222 .pru_sockaddr = in_getsockaddr,
1223 .pru_sosetlabel = in_pcbsosetlabel,
1224 .pru_close = tcp_usr_close,
1229 struct pr_usrreqs tcp6_usrreqs = {
1230 .pru_abort = tcp_usr_abort,
1231 .pru_accept = tcp6_usr_accept,
1232 .pru_attach = tcp_usr_attach,
1233 .pru_bind = tcp6_usr_bind,
1234 .pru_connect = tcp6_usr_connect,
1235 .pru_control = in6_control,
1236 .pru_detach = tcp_usr_detach,
1237 .pru_disconnect = tcp_usr_disconnect,
1238 .pru_listen = tcp6_usr_listen,
1239 .pru_peeraddr = in6_mapped_peeraddr,
1240 .pru_rcvd = tcp_usr_rcvd,
1241 .pru_rcvoob = tcp_usr_rcvoob,
1242 .pru_send = tcp_usr_send,
1243 .pru_ready = tcp_usr_ready,
1244 .pru_shutdown = tcp_usr_shutdown,
1245 .pru_sockaddr = in6_mapped_sockaddr,
1246 .pru_sosetlabel = in_pcbsosetlabel,
1247 .pru_close = tcp_usr_close,
1253 * Common subroutine to open a TCP connection to remote host specified
1254 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1255 * port number if needed. Call in_pcbconnect_setup to do the routing and
1256 * to choose a local host address (interface). If there is an existing
1257 * incarnation of the same connection in TIME-WAIT state and if the remote
1258 * host was sending CC options and if the connection duration was < MSL, then
1259 * truncate the previous TIME-WAIT state and proceed.
1260 * Initialize connection parameters and enter SYN-SENT state.
1263 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1265 struct inpcb *inp = tp->t_inpcb, *oinp;
1266 struct socket *so = inp->inp_socket;
1267 struct in_addr laddr;
1271 INP_WLOCK_ASSERT(inp);
1272 INP_HASH_WLOCK(&V_tcbinfo);
1274 if (inp->inp_lport == 0) {
1275 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1281 * Cannot simply call in_pcbconnect, because there might be an
1282 * earlier incarnation of this same connection still in
1283 * TIME_WAIT state, creating an ADDRINUSE error.
1285 laddr = inp->inp_laddr;
1286 lport = inp->inp_lport;
1287 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1288 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1289 if (error && oinp == NULL)
1295 inp->inp_laddr = laddr;
1297 INP_HASH_WUNLOCK(&V_tcbinfo);
1300 * Compute window scaling to request:
1301 * Scale to fit into sweet spot. See tcp_syncache.c.
1302 * XXX: This should move to tcp_output().
1304 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1305 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1306 tp->request_r_scale++;
1309 TCPSTAT_INC(tcps_connattempt);
1310 tcp_state_change(tp, TCPS_SYN_SENT);
1311 tp->iss = tcp_new_isn(tp);
1312 tcp_sendseqinit(tp);
1317 INP_HASH_WUNLOCK(&V_tcbinfo);
1324 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1326 struct inpcb *inp = tp->t_inpcb;
1329 INP_WLOCK_ASSERT(inp);
1330 INP_HASH_WLOCK(&V_tcbinfo);
1332 if (inp->inp_lport == 0) {
1333 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1337 error = in6_pcbconnect(inp, nam, td->td_ucred);
1340 INP_HASH_WUNLOCK(&V_tcbinfo);
1342 /* Compute window scaling to request. */
1343 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1344 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1345 tp->request_r_scale++;
1347 soisconnecting(inp->inp_socket);
1348 TCPSTAT_INC(tcps_connattempt);
1349 tcp_state_change(tp, TCPS_SYN_SENT);
1350 tp->iss = tcp_new_isn(tp);
1351 tcp_sendseqinit(tp);
1356 INP_HASH_WUNLOCK(&V_tcbinfo);
1362 * Export TCP internal state information via a struct tcp_info, based on the
1363 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1364 * (TCP state machine, etc). We export all information using FreeBSD-native
1365 * constants -- for example, the numeric values for tcpi_state will differ
1369 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1372 INP_WLOCK_ASSERT(tp->t_inpcb);
1373 bzero(ti, sizeof(*ti));
1375 ti->tcpi_state = tp->t_state;
1376 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1377 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1378 if (tp->t_flags & TF_SACK_PERMIT)
1379 ti->tcpi_options |= TCPI_OPT_SACK;
1380 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1381 ti->tcpi_options |= TCPI_OPT_WSCALE;
1382 ti->tcpi_snd_wscale = tp->snd_scale;
1383 ti->tcpi_rcv_wscale = tp->rcv_scale;
1385 if (tp->t_flags & TF_ECN_PERMIT)
1386 ti->tcpi_options |= TCPI_OPT_ECN;
1388 ti->tcpi_rto = tp->t_rxtcur * tick;
1389 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1390 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1391 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1393 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1394 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1397 * FreeBSD-specific extension fields for tcp_info.
1399 ti->tcpi_rcv_space = tp->rcv_wnd;
1400 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1401 ti->tcpi_snd_wnd = tp->snd_wnd;
1402 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1403 ti->tcpi_snd_nxt = tp->snd_nxt;
1404 ti->tcpi_snd_mss = tp->t_maxseg;
1405 ti->tcpi_rcv_mss = tp->t_maxseg;
1406 if (tp->t_flags & TF_TOE)
1407 ti->tcpi_options |= TCPI_OPT_TOE;
1408 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1409 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1410 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1414 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1415 * socket option arguments. When it re-acquires the lock after the copy, it
1416 * has to revalidate that the connection is still valid for the socket
1419 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1421 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1424 return (ECONNRESET); \
1426 tp = intotcpcb(inp); \
1428 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1431 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1436 struct tcp_function_block *blk;
1437 struct tcp_function_set fsn;
1440 inp = sotoinpcb(so);
1441 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1443 if (sopt->sopt_level != IPPROTO_TCP) {
1445 if (inp->inp_vflag & INP_IPV6PROTO) {
1447 error = ip6_ctloutput(so, sopt);
1449 * In case of the IPV6_USE_MIN_MTU socket option,
1450 * the INC_IPV6MINMTU flag to announce a corresponding
1451 * MSS during the initial handshake.
1452 * If the TCP connection is not in the front states,
1453 * just reduce the MSS being used.
1454 * This avoids the sending of TCP segments which will
1455 * be fragmented at the IPv6 layer.
1458 (sopt->sopt_dir == SOPT_SET) &&
1459 (sopt->sopt_level == IPPROTO_IPV6) &&
1460 (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
1462 if ((inp->inp_flags &
1463 (INP_TIMEWAIT | INP_DROPPED))) {
1465 return (ECONNRESET);
1467 inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
1468 tp = intotcpcb(inp);
1469 if ((tp->t_state >= TCPS_SYN_SENT) &&
1470 (inp->inp_inc.inc_flags & INC_ISIPV6)) {
1471 struct ip6_pktopts *opt;
1473 opt = inp->in6p_outputopts;
1474 if ((opt != NULL) &&
1475 (opt->ip6po_minmtu ==
1476 IP6PO_MINMTU_ALL)) {
1477 if (tp->t_maxseg > TCP6_MSS) {
1478 tp->t_maxseg = TCP6_MSS;
1486 #if defined(INET6) && defined(INET)
1492 error = ip_ctloutput(so, sopt);
1497 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1499 return (ECONNRESET);
1501 tp = intotcpcb(inp);
1503 * Protect the TCP option TCP_FUNCTION_BLK so
1504 * that a sub-function can *never* overwrite this.
1506 if ((sopt->sopt_dir == SOPT_SET) &&
1507 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1509 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1513 INP_WLOCK_RECHECK(inp);
1514 if (tp->t_state != TCPS_CLOSED) {
1516 * The user has advanced the state
1517 * past the initial point, we can't
1518 * switch since we are down the road
1519 * and a new set of functions may
1520 * not be compatibile.
1525 blk = find_and_ref_tcp_functions(&fsn);
1530 if (tp->t_fb != blk) {
1531 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1532 refcount_release(&blk->tfb_refcnt);
1537 * Release the old refcnt, the
1538 * lookup acquires a ref on the
1541 if (tp->t_fb->tfb_tcp_fb_fini)
1542 (*tp->t_fb->tfb_tcp_fb_fini)(tp);
1543 refcount_release(&tp->t_fb->tfb_refcnt);
1545 if (tp->t_fb->tfb_tcp_fb_init) {
1546 (*tp->t_fb->tfb_tcp_fb_init)(tp);
1550 if (tp->t_flags & TF_TOE) {
1551 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1557 } else if ((sopt->sopt_dir == SOPT_GET) &&
1558 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1559 strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1560 TCP_FUNCTION_NAME_LEN_MAX);
1561 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1562 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1564 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1567 /* Pass in the INP locked, called must unlock it */
1568 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1572 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1574 int error, opt, optval;
1577 struct cc_algo *algo;
1578 char *pbuf, buf[TCP_CA_NAME_MAX];
1582 * For TCP_CCALGOOPT forward the control to CC module, for both
1583 * SOPT_SET and SOPT_GET.
1585 switch (sopt->sopt_name) {
1588 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
1590 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1591 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1592 sopt->sopt_valsize);
1597 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1598 if (CC_ALGO(tp)->ctl_output != NULL)
1599 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1603 if (error == 0 && sopt->sopt_dir == SOPT_GET)
1604 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1609 switch (sopt->sopt_dir) {
1611 switch (sopt->sopt_name) {
1612 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1614 if (!TCPMD5_ENABLED()) {
1616 return (ENOPROTOOPT);
1618 error = TCPMD5_PCBCTL(inp, sopt);
1621 goto unlock_and_done;
1627 error = sooptcopyin(sopt, &optval, sizeof optval,
1632 INP_WLOCK_RECHECK(inp);
1633 switch (sopt->sopt_name) {
1641 opt = 0; /* dead code to fool gcc */
1648 tp->t_flags &= ~opt;
1651 if (tp->t_flags & TF_TOE) {
1652 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1661 error = sooptcopyin(sopt, &optval, sizeof optval,
1666 INP_WLOCK_RECHECK(inp);
1668 tp->t_flags |= TF_NOPUSH;
1669 else if (tp->t_flags & TF_NOPUSH) {
1670 tp->t_flags &= ~TF_NOPUSH;
1671 if (TCPS_HAVEESTABLISHED(tp->t_state))
1672 error = tp->t_fb->tfb_tcp_output(tp);
1674 goto unlock_and_done;
1678 error = sooptcopyin(sopt, &optval, sizeof optval,
1683 INP_WLOCK_RECHECK(inp);
1684 if (optval > 0 && optval <= tp->t_maxseg &&
1685 optval + 40 >= V_tcp_minmss)
1686 tp->t_maxseg = optval;
1689 goto unlock_and_done;
1696 case TCP_CONGESTION:
1698 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1701 buf[sopt->sopt_valsize] = '\0';
1702 INP_WLOCK_RECHECK(inp);
1704 STAILQ_FOREACH(algo, &cc_list, entries)
1705 if (strncmp(buf, algo->name,
1706 TCP_CA_NAME_MAX) == 0)
1715 * We hold a write lock over the tcb so it's safe to
1716 * do these things without ordering concerns.
1718 if (CC_ALGO(tp)->cb_destroy != NULL)
1719 CC_ALGO(tp)->cb_destroy(tp->ccv);
1722 * If something goes pear shaped initialising the new
1723 * algo, fall back to newreno (which does not
1724 * require initialisation).
1726 if (algo->cb_init != NULL &&
1727 algo->cb_init(tp->ccv) != 0) {
1728 CC_ALGO(tp) = &newreno_cc_algo;
1730 * The only reason init should fail is
1731 * because of malloc.
1742 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1746 if (ui > (UINT_MAX / hz)) {
1752 INP_WLOCK_RECHECK(inp);
1753 switch (sopt->sopt_name) {
1755 tp->t_keepidle = ui;
1757 * XXX: better check current remaining
1758 * timeout and "merge" it with new value.
1760 if ((tp->t_state > TCPS_LISTEN) &&
1761 (tp->t_state <= TCPS_CLOSING))
1762 tcp_timer_activate(tp, TT_KEEP,
1766 tp->t_keepintvl = ui;
1767 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1768 (TP_MAXIDLE(tp) > 0))
1769 tcp_timer_activate(tp, TT_2MSL,
1773 tp->t_keepinit = ui;
1774 if (tp->t_state == TCPS_SYN_RECEIVED ||
1775 tp->t_state == TCPS_SYN_SENT)
1776 tcp_timer_activate(tp, TT_KEEP,
1780 goto unlock_and_done;
1784 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1788 INP_WLOCK_RECHECK(inp);
1790 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1791 (TP_MAXIDLE(tp) > 0))
1792 tcp_timer_activate(tp, TT_2MSL,
1794 goto unlock_and_done;
1800 error = sooptcopyin(sopt, &optval, sizeof optval,
1805 INP_WLOCK_RECHECK(inp);
1807 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
1808 &(tp->t_outpkts) : &(tp->t_inpkts),
1812 goto unlock_and_done;
1818 if (!V_tcp_fastopen_enabled)
1821 error = sooptcopyin(sopt, &optval, sizeof optval,
1826 INP_WLOCK_RECHECK(inp);
1828 tp->t_flags |= TF_FASTOPEN;
1829 if ((tp->t_state == TCPS_LISTEN) &&
1830 (tp->t_tfo_pending == NULL))
1832 tcp_fastopen_alloc_counter();
1834 tp->t_flags &= ~TF_FASTOPEN;
1835 goto unlock_and_done;
1840 error = ENOPROTOOPT;
1846 tp = intotcpcb(inp);
1847 switch (sopt->sopt_name) {
1848 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1850 if (!TCPMD5_ENABLED()) {
1852 return (ENOPROTOOPT);
1854 error = TCPMD5_PCBCTL(inp, sopt);
1859 optval = tp->t_flags & TF_NODELAY;
1861 error = sooptcopyout(sopt, &optval, sizeof optval);
1864 optval = tp->t_maxseg;
1866 error = sooptcopyout(sopt, &optval, sizeof optval);
1869 optval = tp->t_flags & TF_NOOPT;
1871 error = sooptcopyout(sopt, &optval, sizeof optval);
1874 optval = tp->t_flags & TF_NOPUSH;
1876 error = sooptcopyout(sopt, &optval, sizeof optval);
1879 tcp_fill_info(tp, &ti);
1881 error = sooptcopyout(sopt, &ti, sizeof ti);
1883 case TCP_CONGESTION:
1884 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1886 error = sooptcopyout(sopt, buf, len + 1);
1892 switch (sopt->sopt_name) {
1894 ui = TP_KEEPIDLE(tp) / hz;
1897 ui = TP_KEEPINTVL(tp) / hz;
1900 ui = TP_KEEPINIT(tp) / hz;
1903 ui = TP_KEEPCNT(tp);
1907 error = sooptcopyout(sopt, &ui, sizeof(ui));
1912 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
1913 &(tp->t_outpkts) : &(tp->t_inpkts));
1915 error = sooptcopyout(sopt, &optval, sizeof optval);
1921 optval = tp->t_flags & TF_FASTOPEN;
1923 error = sooptcopyout(sopt, &optval, sizeof optval);
1928 error = ENOPROTOOPT;
1935 #undef INP_WLOCK_RECHECK
1936 #undef INP_WLOCK_RECHECK_CLEANUP
1939 * Attach TCP protocol to socket, allocating
1940 * internet protocol control block, tcp control block,
1941 * bufer space, and entering LISTEN state if to accept connections.
1944 tcp_attach(struct socket *so)
1950 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1951 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1955 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1956 so->so_snd.sb_flags |= SB_AUTOSIZE;
1957 INP_INFO_RLOCK(&V_tcbinfo);
1958 error = in_pcballoc(so, &V_tcbinfo);
1960 INP_INFO_RUNLOCK(&V_tcbinfo);
1963 inp = sotoinpcb(so);
1965 if (inp->inp_vflag & INP_IPV6PROTO) {
1966 inp->inp_vflag |= INP_IPV6;
1967 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
1968 inp->inp_vflag |= INP_IPV4;
1969 inp->in6p_hops = -1; /* use kernel default */
1973 inp->inp_vflag |= INP_IPV4;
1974 tp = tcp_newtcpcb(inp);
1978 INP_INFO_RUNLOCK(&V_tcbinfo);
1981 tp->t_state = TCPS_CLOSED;
1983 INP_INFO_RUNLOCK(&V_tcbinfo);
1984 TCPSTATES_INC(TCPS_CLOSED);
1989 * Initiate (or continue) disconnect.
1990 * If embryonic state, just send reset (once).
1991 * If in ``let data drain'' option and linger null, just drop.
1992 * Otherwise (hard), mark socket disconnecting and drop
1993 * current input data; switch states based on user close, and
1994 * send segment to peer (with FIN).
1997 tcp_disconnect(struct tcpcb *tp)
1999 struct inpcb *inp = tp->t_inpcb;
2000 struct socket *so = inp->inp_socket;
2002 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2003 INP_WLOCK_ASSERT(inp);
2006 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2007 * socket is still open.
2009 if (tp->t_state < TCPS_ESTABLISHED) {
2012 ("tcp_disconnect: tcp_close() returned NULL"));
2013 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2014 tp = tcp_drop(tp, 0);
2016 ("tcp_disconnect: tcp_drop() returned NULL"));
2018 soisdisconnecting(so);
2019 sbflush(&so->so_rcv);
2021 if (!(inp->inp_flags & INP_DROPPED))
2022 tp->t_fb->tfb_tcp_output(tp);
2027 * User issued close, and wish to trail through shutdown states:
2028 * if never received SYN, just forget it. If got a SYN from peer,
2029 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2030 * If already got a FIN from peer, then almost done; go to LAST_ACK
2031 * state. In all other cases, have already sent FIN to peer (e.g.
2032 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2033 * for peer to send FIN or not respond to keep-alives, etc.
2034 * We can let the user exit from the close as soon as the FIN is acked.
2037 tcp_usrclosed(struct tcpcb *tp)
2040 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2041 INP_WLOCK_ASSERT(tp->t_inpcb);
2043 switch (tp->t_state) {
2046 tcp_offload_listen_stop(tp);
2048 tcp_state_change(tp, TCPS_CLOSED);
2053 * tcp_close() should never return NULL here as the socket is
2057 ("tcp_usrclosed: tcp_close() returned NULL"));
2061 case TCPS_SYN_RECEIVED:
2062 tp->t_flags |= TF_NEEDFIN;
2065 case TCPS_ESTABLISHED:
2066 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2069 case TCPS_CLOSE_WAIT:
2070 tcp_state_change(tp, TCPS_LAST_ACK);
2073 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2074 soisdisconnected(tp->t_inpcb->inp_socket);
2075 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2076 if (tp->t_state == TCPS_FIN_WAIT_2) {
2079 timeout = (tcp_fast_finwait2_recycle) ?
2080 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2081 tcp_timer_activate(tp, TT_2MSL, timeout);
2088 db_print_indent(int indent)
2092 for (i = 0; i < indent; i++)
2097 db_print_tstate(int t_state)
2102 db_printf("TCPS_CLOSED");
2106 db_printf("TCPS_LISTEN");
2110 db_printf("TCPS_SYN_SENT");
2113 case TCPS_SYN_RECEIVED:
2114 db_printf("TCPS_SYN_RECEIVED");
2117 case TCPS_ESTABLISHED:
2118 db_printf("TCPS_ESTABLISHED");
2121 case TCPS_CLOSE_WAIT:
2122 db_printf("TCPS_CLOSE_WAIT");
2125 case TCPS_FIN_WAIT_1:
2126 db_printf("TCPS_FIN_WAIT_1");
2130 db_printf("TCPS_CLOSING");
2134 db_printf("TCPS_LAST_ACK");
2137 case TCPS_FIN_WAIT_2:
2138 db_printf("TCPS_FIN_WAIT_2");
2141 case TCPS_TIME_WAIT:
2142 db_printf("TCPS_TIME_WAIT");
2146 db_printf("unknown");
2152 db_print_tflags(u_int t_flags)
2157 if (t_flags & TF_ACKNOW) {
2158 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2161 if (t_flags & TF_DELACK) {
2162 db_printf("%sTF_DELACK", comma ? ", " : "");
2165 if (t_flags & TF_NODELAY) {
2166 db_printf("%sTF_NODELAY", comma ? ", " : "");
2169 if (t_flags & TF_NOOPT) {
2170 db_printf("%sTF_NOOPT", comma ? ", " : "");
2173 if (t_flags & TF_SENTFIN) {
2174 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2177 if (t_flags & TF_REQ_SCALE) {
2178 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2181 if (t_flags & TF_RCVD_SCALE) {
2182 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2185 if (t_flags & TF_REQ_TSTMP) {
2186 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2189 if (t_flags & TF_RCVD_TSTMP) {
2190 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2193 if (t_flags & TF_SACK_PERMIT) {
2194 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2197 if (t_flags & TF_NEEDSYN) {
2198 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2201 if (t_flags & TF_NEEDFIN) {
2202 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2205 if (t_flags & TF_NOPUSH) {
2206 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2209 if (t_flags & TF_MORETOCOME) {
2210 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2213 if (t_flags & TF_LQ_OVERFLOW) {
2214 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2217 if (t_flags & TF_LASTIDLE) {
2218 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2221 if (t_flags & TF_RXWIN0SENT) {
2222 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2225 if (t_flags & TF_FASTRECOVERY) {
2226 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2229 if (t_flags & TF_CONGRECOVERY) {
2230 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2233 if (t_flags & TF_WASFRECOVERY) {
2234 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2237 if (t_flags & TF_SIGNATURE) {
2238 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2241 if (t_flags & TF_FORCEDATA) {
2242 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2245 if (t_flags & TF_TSO) {
2246 db_printf("%sTF_TSO", comma ? ", " : "");
2249 if (t_flags & TF_ECN_PERMIT) {
2250 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2253 if (t_flags & TF_FASTOPEN) {
2254 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2260 db_print_toobflags(char t_oobflags)
2265 if (t_oobflags & TCPOOB_HAVEDATA) {
2266 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2269 if (t_oobflags & TCPOOB_HADDATA) {
2270 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2276 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2279 db_print_indent(indent);
2280 db_printf("%s at %p\n", name, tp);
2284 db_print_indent(indent);
2285 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2286 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2288 db_print_indent(indent);
2289 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2290 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2292 db_print_indent(indent);
2293 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2294 &tp->t_timers->tt_delack, tp->t_inpcb);
2296 db_print_indent(indent);
2297 db_printf("t_state: %d (", tp->t_state);
2298 db_print_tstate(tp->t_state);
2301 db_print_indent(indent);
2302 db_printf("t_flags: 0x%x (", tp->t_flags);
2303 db_print_tflags(tp->t_flags);
2306 db_print_indent(indent);
2307 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2308 tp->snd_una, tp->snd_max, tp->snd_nxt);
2310 db_print_indent(indent);
2311 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2312 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2314 db_print_indent(indent);
2315 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2316 tp->iss, tp->irs, tp->rcv_nxt);
2318 db_print_indent(indent);
2319 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
2320 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2322 db_print_indent(indent);
2323 db_printf("snd_wnd: %lu snd_cwnd: %lu\n",
2324 tp->snd_wnd, tp->snd_cwnd);
2326 db_print_indent(indent);
2327 db_printf("snd_ssthresh: %lu snd_recover: "
2328 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2330 db_print_indent(indent);
2331 db_printf("t_rcvtime: %u t_startime: %u\n",
2332 tp->t_rcvtime, tp->t_starttime);
2334 db_print_indent(indent);
2335 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2336 tp->t_rtttime, tp->t_rtseq);
2338 db_print_indent(indent);
2339 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2340 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2342 db_print_indent(indent);
2343 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2344 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2347 db_print_indent(indent);
2348 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
2349 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2351 db_print_indent(indent);
2352 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2353 db_print_toobflags(tp->t_oobflags);
2354 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2356 db_print_indent(indent);
2357 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2358 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2360 db_print_indent(indent);
2361 db_printf("ts_recent: %u ts_recent_age: %u\n",
2362 tp->ts_recent, tp->ts_recent_age);
2364 db_print_indent(indent);
2365 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2366 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2368 db_print_indent(indent);
2369 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
2370 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2371 tp->snd_recover_prev, tp->t_badrxtwin);
2373 db_print_indent(indent);
2374 db_printf("snd_numholes: %d snd_holes first: %p\n",
2375 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2377 db_print_indent(indent);
2378 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2379 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2381 /* Skip sackblks, sackhint. */
2383 db_print_indent(indent);
2384 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2385 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2388 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2393 db_printf("usage: show tcpcb <addr>\n");
2396 tp = (struct tcpcb *)addr;
2398 db_print_tcpcb(tp, "tcpcb", 0);