1c398230bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1993 5623dce13SRobert Watson * The Regents of the University of California. 6497057eeSRobert Watson * Copyright (c) 2006-2007 Robert N. M. Watson 7fa046d87SRobert Watson * Copyright (c) 2010-2011 Juniper Networks, Inc. 8623dce13SRobert Watson * All rights reserved. 9df8bae1dSRodney W. Grimes * 10fa046d87SRobert Watson * Portions of this software were developed by Robert N. M. Watson under 11fa046d87SRobert Watson * contract to Juniper Networks, Inc. 12fa046d87SRobert Watson * 13df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 14df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 15df8bae1dSRodney W. Grimes * are met: 16df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 17df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 18df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 19df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 20df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 21fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 22df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 23df8bae1dSRodney W. Grimes * without specific prior written permission. 24df8bae1dSRodney W. Grimes * 25df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35df8bae1dSRodney W. Grimes * SUCH DAMAGE. 36df8bae1dSRodney W. Grimes * 371fdbc7aeSGarrett Wollman * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 38df8bae1dSRodney W. Grimes */ 39df8bae1dSRodney W. Grimes 404b421e2dSMike Silbersack #include <sys/cdefs.h> 414b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 424b421e2dSMike Silbersack 43497057eeSRobert Watson #include "opt_ddb.h" 441cfd4b53SBruce M Simpson #include "opt_inet.h" 45fb59c426SYoshinobu Inoue #include "opt_inet6.h" 46fcf59617SAndrey V. Elsukov #include "opt_ipsec.h" 47b2e60773SJohn Baldwin #include "opt_kern_tls.h" 480cc12cc5SJoerg Wunsch 49df8bae1dSRodney W. Grimes #include <sys/param.h> 50df8bae1dSRodney W. Grimes #include <sys/systm.h> 51adc56f5aSEdward Tomasz Napierala #include <sys/arb.h> 529077f387SGleb Smirnoff #include <sys/limits.h> 53f76fcf6dSJeffrey Hsu #include <sys/malloc.h> 5455bceb1eSRandall Stewart #include <sys/refcount.h> 55c7a82f90SGarrett Wollman #include <sys/kernel.h> 56b2e60773SJohn Baldwin #include <sys/ktls.h> 57adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h> 5898163b98SPoul-Henning Kamp #include <sys/sysctl.h> 59df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 60fb59c426SYoshinobu Inoue #ifdef INET6 61fb59c426SYoshinobu Inoue #include <sys/domain.h> 62fb59c426SYoshinobu Inoue #endif /* INET6 */ 63df8bae1dSRodney W. Grimes #include <sys/socket.h> 64df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 65df8bae1dSRodney W. Grimes #include <sys/protosw.h> 6691421ba2SRobert Watson #include <sys/proc.h> 6791421ba2SRobert Watson #include <sys/jail.h> 68adc56f5aSEdward Tomasz Napierala #include <sys/stats.h> 69df8bae1dSRodney W. Grimes 70497057eeSRobert Watson #ifdef DDB 71497057eeSRobert Watson #include <ddb/ddb.h> 72497057eeSRobert Watson #endif 73497057eeSRobert Watson 74df8bae1dSRodney W. Grimes #include <net/if.h> 7576039bc8SGleb Smirnoff #include <net/if_var.h> 76df8bae1dSRodney W. Grimes #include <net/route.h> 77530c0060SRobert Watson #include <net/vnet.h> 78df8bae1dSRodney W. Grimes 79df8bae1dSRodney W. Grimes #include <netinet/in.h> 805d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h> 81df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 82b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h> 83b5e8ce9fSBruce Evans #include <netinet/in_var.h> 843b0ee680SRichard Scheffenegger #include <netinet/ip.h> 85df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 86fb59c426SYoshinobu Inoue #ifdef INET6 87b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h> 88b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 89fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 90a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h> 91fb59c426SYoshinobu Inoue #endif 922de3e790SGleb Smirnoff #include <netinet/tcp.h> 93df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 94df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 95df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 96df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 972529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h> 98df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 994644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 100c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h> 101fd389e7cSRandall Stewart #include <netinet/tcp_hpts.h> 10286a996e6SHiren Panchasara #ifdef TCPPCAP 10386a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 10486a996e6SHiren Panchasara #endif 10509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 106bc65987aSKip Macy #include <netinet/tcp_offload.h> 10709fe6320SNavdeep Parhar #endif 108fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 109df8bae1dSRodney W. Grimes 110adc56f5aSEdward Tomasz Napierala #include <vm/vm.h> 111adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h> 112adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h> 113adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h> 114adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h> 115adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h> 116adc56f5aSEdward Tomasz Napierala 117df8bae1dSRodney W. Grimes /* 118df8bae1dSRodney W. Grimes * TCP protocol interface to socket abstraction. 119df8bae1dSRodney W. Grimes */ 120b287c6c7SBjoern A. Zeeb #ifdef INET 121a9d22cceSGleb Smirnoff static int tcp_connect(struct tcpcb *, struct sockaddr_in *, 1224d77a549SAlfred Perlstein struct thread *td); 123b287c6c7SBjoern A. Zeeb #endif /* INET */ 124fb59c426SYoshinobu Inoue #ifdef INET6 125a9d22cceSGleb Smirnoff static int tcp6_connect(struct tcpcb *, struct sockaddr_in6 *, 1264d77a549SAlfred Perlstein struct thread *td); 127fb59c426SYoshinobu Inoue #endif /* INET6 */ 128623dce13SRobert Watson static void tcp_disconnect(struct tcpcb *); 129623dce13SRobert Watson static void tcp_usrclosed(struct tcpcb *); 130b8af5dfaSRobert Watson static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 1312c37256eSGarrett Wollman 132d3b6c96bSRandall Stewart static int tcp_pru_options_support(struct tcpcb *tp, int flags); 133d3b6c96bSRandall Stewart 1342c37256eSGarrett Wollman /* 1352c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 1362c37256eSGarrett Wollman * and an internet control block. 1372c37256eSGarrett Wollman */ 1382c37256eSGarrett Wollman static int 139b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td) 1402c37256eSGarrett Wollman { 141f76fcf6dSJeffrey Hsu struct inpcb *inp; 142623dce13SRobert Watson struct tcpcb *tp = NULL; 143623dce13SRobert Watson int error; 1442c37256eSGarrett Wollman 145623dce13SRobert Watson inp = sotoinpcb(so); 146623dce13SRobert Watson KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 1472c37256eSGarrett Wollman 1480f6385e7SGleb Smirnoff error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace); 1492c37256eSGarrett Wollman if (error) 1502c37256eSGarrett Wollman goto out; 1512c37256eSGarrett Wollman 1520f6385e7SGleb Smirnoff so->so_rcv.sb_flags |= SB_AUTOSIZE; 1530f6385e7SGleb Smirnoff so->so_snd.sb_flags |= SB_AUTOSIZE; 1540f6385e7SGleb Smirnoff error = in_pcballoc(so, &V_tcbinfo); 1557669c586SGleb Smirnoff if (error) 1560f6385e7SGleb Smirnoff goto out; 1570f6385e7SGleb Smirnoff inp = sotoinpcb(so); 1580f6385e7SGleb Smirnoff tp = tcp_newtcpcb(inp); 1590f6385e7SGleb Smirnoff if (tp == NULL) { 1607669c586SGleb Smirnoff error = ENOBUFS; 1610f6385e7SGleb Smirnoff in_pcbdetach(inp); 1620f6385e7SGleb Smirnoff in_pcbfree(inp); 1630f6385e7SGleb Smirnoff goto out; 1640f6385e7SGleb Smirnoff } 1650f6385e7SGleb Smirnoff tp->t_state = TCPS_CLOSED; 1660f6385e7SGleb Smirnoff INP_WUNLOCK(inp); 1670f6385e7SGleb Smirnoff TCPSTATES_INC(TCPS_CLOSED); 1682c37256eSGarrett Wollman out: 1695d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ATTACH); 1700f6385e7SGleb Smirnoff return (error); 1712c37256eSGarrett Wollman } 1722c37256eSGarrett Wollman 1732c37256eSGarrett Wollman /* 1743fed74e9SGleb Smirnoff * tcp_usr_detach is called when the socket layer loses its final reference 175a152f8a3SRobert Watson * to the socket, be it a file descriptor reference, a reference from TCP, 176a152f8a3SRobert Watson * etc. At this point, there is only one case in which we will keep around 177a152f8a3SRobert Watson * inpcb state: time wait. 1782c37256eSGarrett Wollman */ 179bc725eafSRobert Watson static void 1803fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so) 1812c37256eSGarrett Wollman { 1823fed74e9SGleb Smirnoff struct inpcb *inp; 1832c37256eSGarrett Wollman struct tcpcb *tp; 1842c37256eSGarrett Wollman 1853fed74e9SGleb Smirnoff inp = sotoinpcb(so); 1863fed74e9SGleb Smirnoff KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 1873fed74e9SGleb Smirnoff INP_WLOCK(inp); 1883fed74e9SGleb Smirnoff KASSERT(so->so_pcb == inp && inp->inp_socket == so, 1893fed74e9SGleb Smirnoff ("%s: socket %p inp %p mismatch", __func__, so, inp)); 190953b5606SRobert Watson 191a152f8a3SRobert Watson tp = intotcpcb(inp); 192a152f8a3SRobert Watson 1931b91978fSGleb Smirnoff KASSERT(inp->inp_flags & INP_DROPPED || 1941b91978fSGleb Smirnoff tp->t_state < TCPS_SYN_SENT, 1951b91978fSGleb Smirnoff ("%s: inp %p not dropped or embryonic", __func__, inp)); 1969c3507f9SGleb Smirnoff 197623dce13SRobert Watson tcp_discardcb(tp); 198623dce13SRobert Watson in_pcbdetach(inp); 1990206cdb8SBjoern A. Zeeb in_pcbfree(inp); 200623dce13SRobert Watson } 201c78cbc7bSRobert Watson 202b287c6c7SBjoern A. Zeeb #ifdef INET 2032c37256eSGarrett Wollman /* 2042c37256eSGarrett Wollman * Give the socket an address. 2052c37256eSGarrett Wollman */ 2062c37256eSGarrett Wollman static int 207b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 2082c37256eSGarrett Wollman { 2092c37256eSGarrett Wollman int error = 0; 210f76fcf6dSJeffrey Hsu struct inpcb *inp; 211b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS 212623dce13SRobert Watson struct tcpcb *tp = NULL; 213b338b1fdSMateusz Guzik #endif 2142c37256eSGarrett Wollman struct sockaddr_in *sinp; 2152c37256eSGarrett Wollman 21652710de1SPawel Jakub Dawidek sinp = (struct sockaddr_in *)nam; 217f96603b5SMark Johnston if (nam->sa_family != AF_INET) { 218f96603b5SMark Johnston /* 219f96603b5SMark Johnston * Preserve compatibility with old programs. 220f96603b5SMark Johnston */ 221f96603b5SMark Johnston if (nam->sa_family != AF_UNSPEC || 2223f1f6b6eSMichael Tuexen nam->sa_len < offsetof(struct sockaddr_in, sin_zero) || 223f96603b5SMark Johnston sinp->sin_addr.s_addr != INADDR_ANY) 224f161d294SMark Johnston return (EAFNOSUPPORT); 225f96603b5SMark Johnston nam->sa_family = AF_INET; 226f96603b5SMark Johnston } 22752710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof(*sinp)) 22852710de1SPawel Jakub Dawidek return (EINVAL); 229f161d294SMark Johnston 2302c37256eSGarrett Wollman /* 2312c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 2322c37256eSGarrett Wollman * to them. 2332c37256eSGarrett Wollman */ 234f161d294SMark Johnston if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 23552710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 23652710de1SPawel Jakub Dawidek 237623dce13SRobert Watson inp = sotoinpcb(so); 238623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 2398501a69cSRobert Watson INP_WLOCK(inp); 24053af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 241623dce13SRobert Watson error = EINVAL; 2422c37256eSGarrett Wollman goto out; 243623dce13SRobert Watson } 244b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS 245623dce13SRobert Watson tp = intotcpcb(inp); 246b338b1fdSMateusz Guzik #endif 247fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 248623dce13SRobert Watson error = in_pcbbind(inp, nam, td->td_ucred); 249fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 250623dce13SRobert Watson out: 2515d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 2528501a69cSRobert Watson INP_WUNLOCK(inp); 253623dce13SRobert Watson 254623dce13SRobert Watson return (error); 2552c37256eSGarrett Wollman } 256b287c6c7SBjoern A. Zeeb #endif /* INET */ 2572c37256eSGarrett Wollman 258fb59c426SYoshinobu Inoue #ifdef INET6 259fb59c426SYoshinobu Inoue static int 260b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 261fb59c426SYoshinobu Inoue { 262fb59c426SYoshinobu Inoue int error = 0; 263f76fcf6dSJeffrey Hsu struct inpcb *inp; 264b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS 265623dce13SRobert Watson struct tcpcb *tp = NULL; 266b338b1fdSMateusz Guzik #endif 2670ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 2684a91aa8fSMichael Tuexen u_char vflagsav; 269fb59c426SYoshinobu Inoue 2700ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 271f161d294SMark Johnston if (nam->sa_family != AF_INET6) 272f161d294SMark Johnston return (EAFNOSUPPORT); 2730ecd976eSBjoern A. Zeeb if (nam->sa_len != sizeof(*sin6)) 27452710de1SPawel Jakub Dawidek return (EINVAL); 275f161d294SMark Johnston 276fb59c426SYoshinobu Inoue /* 277fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 278fb59c426SYoshinobu Inoue * to them. 279fb59c426SYoshinobu Inoue */ 280f161d294SMark Johnston if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 28152710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 28252710de1SPawel Jakub Dawidek 283623dce13SRobert Watson inp = sotoinpcb(so); 284623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 2858501a69cSRobert Watson INP_WLOCK(inp); 2864a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 28753af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 288623dce13SRobert Watson error = EINVAL; 289623dce13SRobert Watson goto out; 290623dce13SRobert Watson } 291b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS 292623dce13SRobert Watson tp = intotcpcb(inp); 293b338b1fdSMateusz Guzik #endif 294fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 295fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 296fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 297b287c6c7SBjoern A. Zeeb #ifdef INET 29866ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 2990ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 300fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 3010ecd976eSBjoern A. Zeeb else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 302fb59c426SYoshinobu Inoue struct sockaddr_in sin; 303fb59c426SYoshinobu Inoue 3040ecd976eSBjoern A. Zeeb in6_sin6_2_sin(&sin, sin6); 305888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 306888973f5SMichael Tuexen error = EAFNOSUPPORT; 307888973f5SMichael Tuexen INP_HASH_WUNLOCK(&V_tcbinfo); 308888973f5SMichael Tuexen goto out; 309888973f5SMichael Tuexen } 310fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 311fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 312b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)&sin, 313b0330ed9SPawel Jakub Dawidek td->td_ucred); 314fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 315fb59c426SYoshinobu Inoue goto out; 316fb59c426SYoshinobu Inoue } 317fb59c426SYoshinobu Inoue } 318b287c6c7SBjoern A. Zeeb #endif 319b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, nam, td->td_ucred); 320fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 321623dce13SRobert Watson out: 3224a91aa8fSMichael Tuexen if (error != 0) 3234a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 3245d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 3258501a69cSRobert Watson INP_WUNLOCK(inp); 326623dce13SRobert Watson return (error); 327fb59c426SYoshinobu Inoue } 328fb59c426SYoshinobu Inoue #endif /* INET6 */ 329fb59c426SYoshinobu Inoue 330b287c6c7SBjoern A. Zeeb #ifdef INET 3312c37256eSGarrett Wollman /* 3322c37256eSGarrett Wollman * Prepare to accept connections. 3332c37256eSGarrett Wollman */ 3342c37256eSGarrett Wollman static int 335d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 3362c37256eSGarrett Wollman { 3372c37256eSGarrett Wollman int error = 0; 338f76fcf6dSJeffrey Hsu struct inpcb *inp; 339623dce13SRobert Watson struct tcpcb *tp = NULL; 3402c37256eSGarrett Wollman 341623dce13SRobert Watson inp = sotoinpcb(so); 342623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 3438501a69cSRobert Watson INP_WLOCK(inp); 34453af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 345623dce13SRobert Watson error = EINVAL; 346623dce13SRobert Watson goto out; 347623dce13SRobert Watson } 348623dce13SRobert Watson tp = intotcpcb(inp); 3490daccb9cSRobert Watson SOCK_LOCK(so); 3500daccb9cSRobert Watson error = solisten_proto_check(so); 351bd4a39ccSMark Johnston if (error != 0) { 352bd4a39ccSMark Johnston SOCK_UNLOCK(so); 353bd4a39ccSMark Johnston goto out; 354bd4a39ccSMark Johnston } 355bd4a39ccSMark Johnston if (inp->inp_lport == 0) { 356fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 357bd4a39ccSMark Johnston error = in_pcbbind(inp, NULL, td->td_ucred); 358fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 359bd4a39ccSMark Johnston } 3600daccb9cSRobert Watson if (error == 0) { 36157f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 362d374e81eSRobert Watson solisten_proto(so, backlog); 36309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 36437cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 36509fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 36609fe6320SNavdeep Parhar #endif 367bd4a39ccSMark Johnston } else { 368bd4a39ccSMark Johnston solisten_proto_abort(so); 3690daccb9cSRobert Watson } 3700daccb9cSRobert Watson SOCK_UNLOCK(so); 371623dce13SRobert Watson 37268bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) 373281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 37418a75309SPatrick Kelsey 375623dce13SRobert Watson out: 3765d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 3778501a69cSRobert Watson INP_WUNLOCK(inp); 378623dce13SRobert Watson return (error); 3792c37256eSGarrett Wollman } 380b287c6c7SBjoern A. Zeeb #endif /* INET */ 3812c37256eSGarrett Wollman 382fb59c426SYoshinobu Inoue #ifdef INET6 383fb59c426SYoshinobu Inoue static int 384d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 385fb59c426SYoshinobu Inoue { 386fb59c426SYoshinobu Inoue int error = 0; 387f76fcf6dSJeffrey Hsu struct inpcb *inp; 388623dce13SRobert Watson struct tcpcb *tp = NULL; 3894a91aa8fSMichael Tuexen u_char vflagsav; 390fb59c426SYoshinobu Inoue 391623dce13SRobert Watson inp = sotoinpcb(so); 392623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 3938501a69cSRobert Watson INP_WLOCK(inp); 39453af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 395623dce13SRobert Watson error = EINVAL; 396623dce13SRobert Watson goto out; 397623dce13SRobert Watson } 3984a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 399623dce13SRobert Watson tp = intotcpcb(inp); 4000daccb9cSRobert Watson SOCK_LOCK(so); 4010daccb9cSRobert Watson error = solisten_proto_check(so); 402bd4a39ccSMark Johnston if (error != 0) { 403bd4a39ccSMark Johnston SOCK_UNLOCK(so); 404bd4a39ccSMark Johnston goto out; 405bd4a39ccSMark Johnston } 406fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 407bd4a39ccSMark Johnston if (inp->inp_lport == 0) { 408fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 40966ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 410fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 411bd4a39ccSMark Johnston error = in6_pcbbind(inp, NULL, td->td_ucred); 412fb59c426SYoshinobu Inoue } 413fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 4140daccb9cSRobert Watson if (error == 0) { 41557f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 416d374e81eSRobert Watson solisten_proto(so, backlog); 41709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 41837cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 41909fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 42009fe6320SNavdeep Parhar #endif 421bd4a39ccSMark Johnston } else { 422bd4a39ccSMark Johnston solisten_proto_abort(so); 4230daccb9cSRobert Watson } 4240daccb9cSRobert Watson SOCK_UNLOCK(so); 425623dce13SRobert Watson 42668bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) 427281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 42818a75309SPatrick Kelsey 4294a91aa8fSMichael Tuexen if (error != 0) 4304a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 4314a91aa8fSMichael Tuexen 432623dce13SRobert Watson out: 4335d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 4348501a69cSRobert Watson INP_WUNLOCK(inp); 435623dce13SRobert Watson return (error); 436fb59c426SYoshinobu Inoue } 437fb59c426SYoshinobu Inoue #endif /* INET6 */ 438fb59c426SYoshinobu Inoue 439b287c6c7SBjoern A. Zeeb #ifdef INET 4402c37256eSGarrett Wollman /* 4412c37256eSGarrett Wollman * Initiate connection to peer. 4422c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 4432c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 4442c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 4452c37256eSGarrett Wollman * Send initial segment on connection. 4462c37256eSGarrett Wollman */ 4472c37256eSGarrett Wollman static int 448b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 4492c37256eSGarrett Wollman { 450109eb549SGleb Smirnoff struct epoch_tracker et; 4512c37256eSGarrett Wollman int error = 0; 452f76fcf6dSJeffrey Hsu struct inpcb *inp; 453623dce13SRobert Watson struct tcpcb *tp = NULL; 4542c37256eSGarrett Wollman struct sockaddr_in *sinp; 4552c37256eSGarrett Wollman 45657bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 457f161d294SMark Johnston if (nam->sa_family != AF_INET) 458f161d294SMark Johnston return (EAFNOSUPPORT); 459e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sinp)) 460e29ef13fSDon Lewis return (EINVAL); 461f161d294SMark Johnston 46252710de1SPawel Jakub Dawidek /* 46352710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 46452710de1SPawel Jakub Dawidek */ 465f161d294SMark Johnston if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 46652710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 467f161d294SMark Johnston if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) 468f903a308SMichael Tuexen return (EACCES); 469b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 470b89e82ddSJamie Gritton return (error); 47175c13541SPoul-Henning Kamp 472623dce13SRobert Watson inp = sotoinpcb(so); 473623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 4748501a69cSRobert Watson INP_WLOCK(inp); 475eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 476eb96dc33SJulien Charbon error = ECONNREFUSED; 477623dce13SRobert Watson goto out; 478623dce13SRobert Watson } 479bd4a39ccSMark Johnston if (SOLISTENING(so)) { 480bd4a39ccSMark Johnston error = EOPNOTSUPP; 481bd4a39ccSMark Johnston goto out; 482bd4a39ccSMark Johnston } 483623dce13SRobert Watson tp = intotcpcb(inp); 484c1604fe4SGleb Smirnoff NET_EPOCH_ENTER(et); 485a9d22cceSGleb Smirnoff if ((error = tcp_connect(tp, sinp, td)) != 0) 486c1604fe4SGleb Smirnoff goto out_in_epoch; 48709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 48809fe6320SNavdeep Parhar if (registered_toedevs > 0 && 48937cc0ecbSNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 49009fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 491c1604fe4SGleb Smirnoff goto out_in_epoch; 49209fe6320SNavdeep Parhar #endif 49309fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 49440fa3e40SGleb Smirnoff error = tcp_output(tp); 4951d41a494SGleb Smirnoff KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()" 4961d41a494SGleb Smirnoff ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error)); 497c1604fe4SGleb Smirnoff out_in_epoch: 498109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 499623dce13SRobert Watson out: 500e79cb051SGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 5018501a69cSRobert Watson INP_WUNLOCK(inp); 502623dce13SRobert Watson return (error); 5032c37256eSGarrett Wollman } 504b287c6c7SBjoern A. Zeeb #endif /* INET */ 5052c37256eSGarrett Wollman 506fb59c426SYoshinobu Inoue #ifdef INET6 507fb59c426SYoshinobu Inoue static int 508b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 509fb59c426SYoshinobu Inoue { 510109eb549SGleb Smirnoff struct epoch_tracker et; 511fb59c426SYoshinobu Inoue int error = 0; 512f76fcf6dSJeffrey Hsu struct inpcb *inp; 513623dce13SRobert Watson struct tcpcb *tp = NULL; 5140ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 5154a91aa8fSMichael Tuexen u_int8_t incflagsav; 5164a91aa8fSMichael Tuexen u_char vflagsav; 517623dce13SRobert Watson 5180ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 519f161d294SMark Johnston if (nam->sa_family != AF_INET6) 520f161d294SMark Johnston return (EAFNOSUPPORT); 5210ecd976eSBjoern A. Zeeb if (nam->sa_len != sizeof (*sin6)) 522e29ef13fSDon Lewis return (EINVAL); 523f161d294SMark Johnston 52452710de1SPawel Jakub Dawidek /* 52552710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 52652710de1SPawel Jakub Dawidek */ 527f161d294SMark Johnston if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 52852710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 529fb59c426SYoshinobu Inoue 530623dce13SRobert Watson inp = sotoinpcb(so); 531623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 5328501a69cSRobert Watson INP_WLOCK(inp); 5334a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 5344a91aa8fSMichael Tuexen incflagsav = inp->inp_inc.inc_flags; 535eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 536eb96dc33SJulien Charbon error = ECONNREFUSED; 537623dce13SRobert Watson goto out; 538623dce13SRobert Watson } 539bd4a39ccSMark Johnston if (SOLISTENING(so)) { 540bd4a39ccSMark Johnston error = EINVAL; 541bd4a39ccSMark Johnston goto out; 542bd4a39ccSMark Johnston } 543623dce13SRobert Watson tp = intotcpcb(inp); 544b287c6c7SBjoern A. Zeeb #ifdef INET 545fa046d87SRobert Watson /* 546fa046d87SRobert Watson * XXXRW: Some confusion: V4/V6 flags relate to binding, and 547fa046d87SRobert Watson * therefore probably require the hash lock, which isn't held here. 548fa046d87SRobert Watson * Is this a significant problem? 549fa046d87SRobert Watson */ 5500ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 551fb59c426SYoshinobu Inoue struct sockaddr_in sin; 552fb59c426SYoshinobu Inoue 553d46a5312SMaxim Konovalov if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 554d46a5312SMaxim Konovalov error = EINVAL; 555d46a5312SMaxim Konovalov goto out; 556d46a5312SMaxim Konovalov } 5575dba6adaSMichael Tuexen if ((inp->inp_vflag & INP_IPV4) == 0) { 5585dba6adaSMichael Tuexen error = EAFNOSUPPORT; 5595dba6adaSMichael Tuexen goto out; 5605dba6adaSMichael Tuexen } 56133841545SHajimu UMEMOTO 5620ecd976eSBjoern A. Zeeb in6_sin6_2_sin(&sin, sin6); 563888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 564888973f5SMichael Tuexen error = EAFNOSUPPORT; 565888973f5SMichael Tuexen goto out; 566888973f5SMichael Tuexen } 567f903a308SMichael Tuexen if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) { 568f903a308SMichael Tuexen error = EACCES; 5692cf21ae5SRandall Stewart goto out; 5702cf21ae5SRandall Stewart } 571b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, 572b89e82ddSJamie Gritton &sin.sin_addr)) != 0) 573413628a7SBjoern A. Zeeb goto out; 5744a91aa8fSMichael Tuexen inp->inp_vflag |= INP_IPV4; 5754a91aa8fSMichael Tuexen inp->inp_vflag &= ~INP_IPV6; 576c1604fe4SGleb Smirnoff NET_EPOCH_ENTER(et); 577a9d22cceSGleb Smirnoff if ((error = tcp_connect(tp, &sin, td)) != 0) 578c1604fe4SGleb Smirnoff goto out_in_epoch; 57909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 58009fe6320SNavdeep Parhar if (registered_toedevs > 0 && 581adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 58209fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 583c1604fe4SGleb Smirnoff goto out_in_epoch; 58409fe6320SNavdeep Parhar #endif 58540fa3e40SGleb Smirnoff error = tcp_output(tp); 586c1604fe4SGleb Smirnoff goto out_in_epoch; 5875dba6adaSMichael Tuexen } else { 5885dba6adaSMichael Tuexen if ((inp->inp_vflag & INP_IPV6) == 0) { 5895dba6adaSMichael Tuexen error = EAFNOSUPPORT; 5905dba6adaSMichael Tuexen goto out; 5915dba6adaSMichael Tuexen } 592fb59c426SYoshinobu Inoue } 593b287c6c7SBjoern A. Zeeb #endif 5944a91aa8fSMichael Tuexen if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0) 5954a91aa8fSMichael Tuexen goto out; 596fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 597fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 598dcdb4371SBjoern A. Zeeb inp->inp_inc.inc_flags |= INC_ISIPV6; 5990773b44eSGleb Smirnoff NET_EPOCH_ENTER(et); 600a9d22cceSGleb Smirnoff if ((error = tcp6_connect(tp, sin6, td)) != 0) 6010773b44eSGleb Smirnoff goto out_in_epoch; 60209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 60309fe6320SNavdeep Parhar if (registered_toedevs > 0 && 604adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 60509fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 6060773b44eSGleb Smirnoff goto out_in_epoch; 60709fe6320SNavdeep Parhar #endif 60809fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 60940fa3e40SGleb Smirnoff error = tcp_output(tp); 610c1604fe4SGleb Smirnoff out_in_epoch: 611109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 612623dce13SRobert Watson out: 6131d41a494SGleb Smirnoff KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()" 6141d41a494SGleb Smirnoff ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error)); 6154a91aa8fSMichael Tuexen /* 6164a91aa8fSMichael Tuexen * If the implicit bind in the connect call fails, restore 6174a91aa8fSMichael Tuexen * the flags we modified. 6184a91aa8fSMichael Tuexen */ 6194a91aa8fSMichael Tuexen if (error != 0 && inp->inp_lport == 0) { 6204a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 6214a91aa8fSMichael Tuexen inp->inp_inc.inc_flags = incflagsav; 6224a91aa8fSMichael Tuexen } 6234a91aa8fSMichael Tuexen 6245d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 6258501a69cSRobert Watson INP_WUNLOCK(inp); 626623dce13SRobert Watson return (error); 627fb59c426SYoshinobu Inoue } 628fb59c426SYoshinobu Inoue #endif /* INET6 */ 629fb59c426SYoshinobu Inoue 6302c37256eSGarrett Wollman /* 6312c37256eSGarrett Wollman * Initiate disconnect from peer. 6322c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 6332c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 6342c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 6352c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 6362c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 6372c37256eSGarrett Wollman * when peer sends FIN and acks ours. 6382c37256eSGarrett Wollman * 6392c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 6402c37256eSGarrett Wollman */ 6412c37256eSGarrett Wollman static int 6422c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 6432c37256eSGarrett Wollman { 644f76fcf6dSJeffrey Hsu struct inpcb *inp; 645623dce13SRobert Watson struct tcpcb *tp = NULL; 6466573d758SMatt Macy struct epoch_tracker et; 647623dce13SRobert Watson int error = 0; 6482c37256eSGarrett Wollman 64997a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 650623dce13SRobert Watson inp = sotoinpcb(so); 651623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 6528501a69cSRobert Watson INP_WLOCK(inp); 653489dcc92SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 65421367f63SSam Leffler error = ECONNRESET; 655623dce13SRobert Watson goto out; 656623dce13SRobert Watson } 657623dce13SRobert Watson tp = intotcpcb(inp); 658623dce13SRobert Watson tcp_disconnect(tp); 659623dce13SRobert Watson out: 6605d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_DISCONNECT); 6618501a69cSRobert Watson INP_WUNLOCK(inp); 66297a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 663623dce13SRobert Watson return (error); 6642c37256eSGarrett Wollman } 6652c37256eSGarrett Wollman 666b287c6c7SBjoern A. Zeeb #ifdef INET 6672c37256eSGarrett Wollman /* 6688296cddfSRobert Watson * Accept a connection. Essentially all the work is done at higher levels; 6698296cddfSRobert Watson * just return the address of the peer, storing through addr. 6702c37256eSGarrett Wollman */ 6712c37256eSGarrett Wollman static int 67257bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 6732c37256eSGarrett Wollman { 6742c37256eSGarrett Wollman int error = 0; 675f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 676b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS 6771db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 678b338b1fdSMateusz Guzik #endif 67926ef6ac4SDon Lewis struct in_addr addr; 68026ef6ac4SDon Lewis in_port_t port = 0; 6812c37256eSGarrett Wollman 6823d2d3ef4SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 6833d2d3ef4SRobert Watson return (ECONNABORTED); 684f76fcf6dSJeffrey Hsu 685f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 686623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 6878501a69cSRobert Watson INP_WLOCK(inp); 68853af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 6893d2d3ef4SRobert Watson error = ECONNABORTED; 690623dce13SRobert Watson goto out; 691623dce13SRobert Watson } 692b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS 6931db24ffbSJonathan Lemon tp = intotcpcb(inp); 694b338b1fdSMateusz Guzik #endif 695f76fcf6dSJeffrey Hsu 696f76fcf6dSJeffrey Hsu /* 69754d642bbSRobert Watson * We inline in_getpeeraddr and COMMON_END here, so that we can 69826ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 69926ef6ac4SDon Lewis * release the lock. 700f76fcf6dSJeffrey Hsu */ 70126ef6ac4SDon Lewis port = inp->inp_fport; 70226ef6ac4SDon Lewis addr = inp->inp_faddr; 703f76fcf6dSJeffrey Hsu 704623dce13SRobert Watson out: 7055d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 7068501a69cSRobert Watson INP_WUNLOCK(inp); 70726ef6ac4SDon Lewis if (error == 0) 70826ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 70926ef6ac4SDon Lewis return error; 7102c37256eSGarrett Wollman } 711b287c6c7SBjoern A. Zeeb #endif /* INET */ 7122c37256eSGarrett Wollman 713fb59c426SYoshinobu Inoue #ifdef INET6 714fb59c426SYoshinobu Inoue static int 715fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 716fb59c426SYoshinobu Inoue { 717f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 718fb59c426SYoshinobu Inoue int error = 0; 719b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS 7201db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 721b338b1fdSMateusz Guzik #endif 72226ef6ac4SDon Lewis struct in_addr addr; 72326ef6ac4SDon Lewis struct in6_addr addr6; 7246573d758SMatt Macy struct epoch_tracker et; 72526ef6ac4SDon Lewis in_port_t port = 0; 72626ef6ac4SDon Lewis int v4 = 0; 727fb59c426SYoshinobu Inoue 728b4470c16SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 729b4470c16SRobert Watson return (ECONNABORTED); 730f76fcf6dSJeffrey Hsu 731f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 732623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 73397a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 7348501a69cSRobert Watson INP_WLOCK(inp); 73553af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 73621367f63SSam Leffler error = ECONNABORTED; 737623dce13SRobert Watson goto out; 738623dce13SRobert Watson } 739b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS 7401db24ffbSJonathan Lemon tp = intotcpcb(inp); 741b338b1fdSMateusz Guzik #endif 742623dce13SRobert Watson 74326ef6ac4SDon Lewis /* 74426ef6ac4SDon Lewis * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 74526ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 74626ef6ac4SDon Lewis * release the lock. 74726ef6ac4SDon Lewis */ 74826ef6ac4SDon Lewis if (inp->inp_vflag & INP_IPV4) { 74926ef6ac4SDon Lewis v4 = 1; 75026ef6ac4SDon Lewis port = inp->inp_fport; 75126ef6ac4SDon Lewis addr = inp->inp_faddr; 75226ef6ac4SDon Lewis } else { 75326ef6ac4SDon Lewis port = inp->inp_fport; 75426ef6ac4SDon Lewis addr6 = inp->in6p_faddr; 75526ef6ac4SDon Lewis } 75626ef6ac4SDon Lewis 757623dce13SRobert Watson out: 7585d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 7598501a69cSRobert Watson INP_WUNLOCK(inp); 76097a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 76126ef6ac4SDon Lewis if (error == 0) { 76226ef6ac4SDon Lewis if (v4) 76326ef6ac4SDon Lewis *nam = in6_v4mapsin6_sockaddr(port, &addr); 76426ef6ac4SDon Lewis else 76526ef6ac4SDon Lewis *nam = in6_sockaddr(port, &addr6); 76626ef6ac4SDon Lewis } 76726ef6ac4SDon Lewis return error; 768fb59c426SYoshinobu Inoue } 769fb59c426SYoshinobu Inoue #endif /* INET6 */ 770f76fcf6dSJeffrey Hsu 771f76fcf6dSJeffrey Hsu /* 7722c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 7732c37256eSGarrett Wollman */ 7742c37256eSGarrett Wollman static int 7752c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 7762c37256eSGarrett Wollman { 7772c37256eSGarrett Wollman int error = 0; 778f76fcf6dSJeffrey Hsu struct inpcb *inp; 779623dce13SRobert Watson struct tcpcb *tp = NULL; 7806573d758SMatt Macy struct epoch_tracker et; 7812c37256eSGarrett Wollman 782623dce13SRobert Watson inp = sotoinpcb(so); 783623dce13SRobert Watson KASSERT(inp != NULL, ("inp == NULL")); 7848501a69cSRobert Watson INP_WLOCK(inp); 78553af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 7860af4ce45SGleb Smirnoff INP_WUNLOCK(inp); 7870af4ce45SGleb Smirnoff return (ECONNRESET); 788623dce13SRobert Watson } 7890af4ce45SGleb Smirnoff tp = intotcpcb(inp); 7900af4ce45SGleb Smirnoff NET_EPOCH_ENTER(et); 7912c37256eSGarrett Wollman socantsendmore(so); 792623dce13SRobert Watson tcp_usrclosed(tp); 793ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 794f64dc2abSGleb Smirnoff error = tcp_output_nodrop(tp); 7955d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN); 796f64dc2abSGleb Smirnoff error = tcp_unlock_or_drop(tp, error); 79797a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 798623dce13SRobert Watson 799623dce13SRobert Watson return (error); 8002c37256eSGarrett Wollman } 8012c37256eSGarrett Wollman 8022c37256eSGarrett Wollman /* 8032c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 8042c37256eSGarrett Wollman */ 8052c37256eSGarrett Wollman static int 8062c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 8072c37256eSGarrett Wollman { 808109eb549SGleb Smirnoff struct epoch_tracker et; 809f76fcf6dSJeffrey Hsu struct inpcb *inp; 810623dce13SRobert Watson struct tcpcb *tp = NULL; 811f64dc2abSGleb Smirnoff int outrv = 0, error = 0; 8122c37256eSGarrett Wollman 813623dce13SRobert Watson inp = sotoinpcb(so); 814623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 8158501a69cSRobert Watson INP_WLOCK(inp); 81653af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 81737a7f557SGleb Smirnoff INP_WUNLOCK(inp); 81837a7f557SGleb Smirnoff return (ECONNRESET); 819623dce13SRobert Watson } 82037a7f557SGleb Smirnoff tp = intotcpcb(inp); 82137a7f557SGleb Smirnoff NET_EPOCH_ENTER(et); 822281a0fd4SPatrick Kelsey /* 823281a0fd4SPatrick Kelsey * For passively-created TFO connections, don't attempt a window 824281a0fd4SPatrick Kelsey * update while still in SYN_RECEIVED as this may trigger an early 825281a0fd4SPatrick Kelsey * SYN|ACK. It is preferable to have the SYN|ACK be sent along with 826281a0fd4SPatrick Kelsey * application response data, or failing that, when the DELACK timer 827281a0fd4SPatrick Kelsey * expires. 828281a0fd4SPatrick Kelsey */ 82968bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 830281a0fd4SPatrick Kelsey (tp->t_state == TCPS_SYN_RECEIVED)) 831281a0fd4SPatrick Kelsey goto out; 83209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 83309fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 83409fe6320SNavdeep Parhar tcp_offload_rcvd(tp); 835460cf046SNavdeep Parhar else 83609fe6320SNavdeep Parhar #endif 837f64dc2abSGleb Smirnoff outrv = tcp_output_nodrop(tp); 838623dce13SRobert Watson out: 8395d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVD); 840f64dc2abSGleb Smirnoff (void) tcp_unlock_or_drop(tp, outrv); 841f64dc2abSGleb Smirnoff NET_EPOCH_EXIT(et); 842623dce13SRobert Watson return (error); 8432c37256eSGarrett Wollman } 8442c37256eSGarrett Wollman 8452c37256eSGarrett Wollman /* 8462c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 8479c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 8489c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 8499c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 8509c9906e9SPeter Wemm * generally are caller-frees. 8512c37256eSGarrett Wollman */ 8522c37256eSGarrett Wollman static int 85357bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 854b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 8552c37256eSGarrett Wollman { 85697a95ee1SGleb Smirnoff struct epoch_tracker et; 8572c37256eSGarrett Wollman int error = 0; 858f76fcf6dSJeffrey Hsu struct inpcb *inp; 859623dce13SRobert Watson struct tcpcb *tp = NULL; 860888973f5SMichael Tuexen #ifdef INET 86151e08d53SMichael Tuexen #ifdef INET6 86251e08d53SMichael Tuexen struct sockaddr_in sin; 86351e08d53SMichael Tuexen #endif 86451e08d53SMichael Tuexen struct sockaddr_in *sinp; 865888973f5SMichael Tuexen #endif 866fb59c426SYoshinobu Inoue #ifdef INET6 867a9d22cceSGleb Smirnoff struct sockaddr_in6 *sin6; 868fb59c426SYoshinobu Inoue int isipv6; 869fb59c426SYoshinobu Inoue #endif 8704a91aa8fSMichael Tuexen u_int8_t incflagsav; 8714a91aa8fSMichael Tuexen u_char vflagsav; 8724a91aa8fSMichael Tuexen bool restoreflags; 8732c37256eSGarrett Wollman 874d8acd268SMark Johnston if (control != NULL) { 875d8acd268SMark Johnston /* TCP doesn't do control messages (rights, creds, etc) */ 876d8acd268SMark Johnston if (control->m_len) { 877d8acd268SMark Johnston m_freem(control); 8784287aa56SGleb Smirnoff return (EINVAL); 879d8acd268SMark Johnston } 880d8acd268SMark Johnston m_freem(control); /* empty control, just free it */ 881d8acd268SMark Johnston } 8824287aa56SGleb Smirnoff 8834287aa56SGleb Smirnoff inp = sotoinpcb(so); 8844287aa56SGleb Smirnoff KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 8854287aa56SGleb Smirnoff INP_WLOCK(inp); 88653af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 8874287aa56SGleb Smirnoff if (m != NULL && (flags & PRUS_NOTREADY) == 0) 8884287aa56SGleb Smirnoff m_freem(m); 8894287aa56SGleb Smirnoff INP_WUNLOCK(inp); 8904287aa56SGleb Smirnoff return (ECONNRESET); 8914287aa56SGleb Smirnoff } 8924287aa56SGleb Smirnoff 8934287aa56SGleb Smirnoff vflagsav = inp->inp_vflag; 8944287aa56SGleb Smirnoff incflagsav = inp->inp_inc.inc_flags; 8954287aa56SGleb Smirnoff restoreflags = false; 8964287aa56SGleb Smirnoff tp = intotcpcb(inp); 8974287aa56SGleb Smirnoff 8984287aa56SGleb Smirnoff NET_EPOCH_ENTER(et); 8997d2608a5SMark Johnston if ((flags & PRUS_OOB) != 0 && 9007d2608a5SMark Johnston (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0) 901d3b6c96bSRandall Stewart goto out; 9027d2608a5SMark Johnston 903888973f5SMichael Tuexen if (nam != NULL && tp->t_state < TCPS_SYN_SENT) { 904bd4a39ccSMark Johnston if (tp->t_state == TCPS_LISTEN) { 905bd4a39ccSMark Johnston error = EINVAL; 906bd4a39ccSMark Johnston goto out; 907bd4a39ccSMark Johnston } 908888973f5SMichael Tuexen switch (nam->sa_family) { 909888973f5SMichael Tuexen #ifdef INET 910888973f5SMichael Tuexen case AF_INET: 911888973f5SMichael Tuexen sinp = (struct sockaddr_in *)nam; 912888973f5SMichael Tuexen if (sinp->sin_len != sizeof(struct sockaddr_in)) { 913888973f5SMichael Tuexen error = EINVAL; 914888973f5SMichael Tuexen goto out; 915888973f5SMichael Tuexen } 916888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV6) != 0) { 917888973f5SMichael Tuexen error = EAFNOSUPPORT; 918888973f5SMichael Tuexen goto out; 919888973f5SMichael Tuexen } 920888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 921888973f5SMichael Tuexen error = EAFNOSUPPORT; 922888973f5SMichael Tuexen goto out; 923888973f5SMichael Tuexen } 924f903a308SMichael Tuexen if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) { 925f903a308SMichael Tuexen error = EACCES; 9262cf21ae5SRandall Stewart goto out; 9272cf21ae5SRandall Stewart } 928888973f5SMichael Tuexen if ((error = prison_remote_ip4(td->td_ucred, 9297d2608a5SMark Johnston &sinp->sin_addr))) 930888973f5SMichael Tuexen goto out; 931888973f5SMichael Tuexen #ifdef INET6 932888973f5SMichael Tuexen isipv6 = 0; 933888973f5SMichael Tuexen #endif 934888973f5SMichael Tuexen break; 935888973f5SMichael Tuexen #endif /* INET */ 936888973f5SMichael Tuexen #ifdef INET6 937888973f5SMichael Tuexen case AF_INET6: 9380ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 9390ecd976eSBjoern A. Zeeb if (sin6->sin6_len != sizeof(*sin6)) { 940888973f5SMichael Tuexen error = EINVAL; 941888973f5SMichael Tuexen goto out; 942888973f5SMichael Tuexen } 943e240ce42SMichael Tuexen if ((inp->inp_vflag & INP_IPV6PROTO) == 0) { 944e240ce42SMichael Tuexen error = EAFNOSUPPORT; 945e240ce42SMichael Tuexen goto out; 946e240ce42SMichael Tuexen } 9470ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 948888973f5SMichael Tuexen error = EAFNOSUPPORT; 949888973f5SMichael Tuexen goto out; 950888973f5SMichael Tuexen } 9510ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 952888973f5SMichael Tuexen #ifdef INET 953888973f5SMichael Tuexen if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 954888973f5SMichael Tuexen error = EINVAL; 955888973f5SMichael Tuexen goto out; 956888973f5SMichael Tuexen } 957888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV4) == 0) { 958888973f5SMichael Tuexen error = EAFNOSUPPORT; 959888973f5SMichael Tuexen goto out; 960888973f5SMichael Tuexen } 9614a91aa8fSMichael Tuexen restoreflags = true; 962888973f5SMichael Tuexen inp->inp_vflag &= ~INP_IPV6; 963888973f5SMichael Tuexen sinp = &sin; 9640ecd976eSBjoern A. Zeeb in6_sin6_2_sin(sinp, sin6); 965888973f5SMichael Tuexen if (IN_MULTICAST( 966888973f5SMichael Tuexen ntohl(sinp->sin_addr.s_addr))) { 967888973f5SMichael Tuexen error = EAFNOSUPPORT; 968888973f5SMichael Tuexen goto out; 969888973f5SMichael Tuexen } 970888973f5SMichael Tuexen if ((error = prison_remote_ip4(td->td_ucred, 9717d2608a5SMark Johnston &sinp->sin_addr))) 972888973f5SMichael Tuexen goto out; 973888973f5SMichael Tuexen isipv6 = 0; 974888973f5SMichael Tuexen #else /* !INET */ 975888973f5SMichael Tuexen error = EAFNOSUPPORT; 976888973f5SMichael Tuexen goto out; 977888973f5SMichael Tuexen #endif /* INET */ 978888973f5SMichael Tuexen } else { 979888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV6) == 0) { 980888973f5SMichael Tuexen error = EAFNOSUPPORT; 981888973f5SMichael Tuexen goto out; 982888973f5SMichael Tuexen } 9834a91aa8fSMichael Tuexen restoreflags = true; 984888973f5SMichael Tuexen inp->inp_vflag &= ~INP_IPV4; 985888973f5SMichael Tuexen inp->inp_inc.inc_flags |= INC_ISIPV6; 986888973f5SMichael Tuexen if ((error = prison_remote_ip6(td->td_ucred, 9877d2608a5SMark Johnston &sin6->sin6_addr))) 988888973f5SMichael Tuexen goto out; 989888973f5SMichael Tuexen isipv6 = 1; 990888973f5SMichael Tuexen } 991888973f5SMichael Tuexen break; 992888973f5SMichael Tuexen #endif /* INET6 */ 993888973f5SMichael Tuexen default: 994888973f5SMichael Tuexen error = EAFNOSUPPORT; 995888973f5SMichael Tuexen goto out; 996888973f5SMichael Tuexen } 997888973f5SMichael Tuexen } 9982c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 99908af8aacSRandall Stewart if (tp->t_acktime == 0) 100008af8aacSRandall Stewart tp->t_acktime = ticks; 1001651e4e6aSGleb Smirnoff sbappendstream(&so->so_snd, m, flags); 10027d2608a5SMark Johnston m = NULL; 10032c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 1004bd4a39ccSMark Johnston KASSERT(tp->t_state == TCPS_CLOSED, 1005bd4a39ccSMark Johnston ("%s: tp %p is listening", __func__, tp)); 1006bd4a39ccSMark Johnston 10072c37256eSGarrett Wollman /* 10082c37256eSGarrett Wollman * Do implied connect if not yet connected, 10092c37256eSGarrett Wollman * initialize window to default value, and 10100c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 10112c37256eSGarrett Wollman */ 1012fb59c426SYoshinobu Inoue #ifdef INET6 1013fb59c426SYoshinobu Inoue if (isipv6) 1014a9d22cceSGleb Smirnoff error = tcp6_connect(tp, sin6, td); 1015fb59c426SYoshinobu Inoue #endif /* INET6 */ 1016b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1017b287c6c7SBjoern A. Zeeb else 1018b287c6c7SBjoern A. Zeeb #endif 1019b287c6c7SBjoern A. Zeeb #ifdef INET 1020a9d22cceSGleb Smirnoff error = tcp_connect(tp, sinp, td); 1021b287c6c7SBjoern A. Zeeb #endif 10224a91aa8fSMichael Tuexen /* 10234a91aa8fSMichael Tuexen * The bind operation in tcp_connect succeeded. We 10244a91aa8fSMichael Tuexen * no longer want to restore the flags if later 10254a91aa8fSMichael Tuexen * operations fail. 10264a91aa8fSMichael Tuexen */ 10274a91aa8fSMichael Tuexen if (error == 0 || inp->inp_lport != 0) 10284a91aa8fSMichael Tuexen restoreflags = false; 10294a91aa8fSMichael Tuexen 10307d2608a5SMark Johnston if (error) { 10317d2608a5SMark Johnston /* m is freed if PRUS_NOTREADY is unset. */ 10327d2608a5SMark Johnston sbflush(&so->so_snd); 10332c37256eSGarrett Wollman goto out; 10347d2608a5SMark Johnston } 1035c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) 1036c560df6fSPatrick Kelsey tcp_fastopen_connect(tp); 103718a75309SPatrick Kelsey else { 10382c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 10392c37256eSGarrett Wollman tcp_mss(tp, -1); 10402c37256eSGarrett Wollman } 1041c560df6fSPatrick Kelsey } 10422c37256eSGarrett Wollman if (flags & PRUS_EOF) { 10432c37256eSGarrett Wollman /* 10442c37256eSGarrett Wollman * Close the send side of the connection after 10452c37256eSGarrett Wollman * the data is sent. 10462c37256eSGarrett Wollman */ 10472c37256eSGarrett Wollman socantsendmore(so); 1048623dce13SRobert Watson tcp_usrclosed(tp); 10492c37256eSGarrett Wollman } 1050e854dd38SRandall Stewart if (TCPS_HAVEESTABLISHED(tp->t_state) && 1051e854dd38SRandall Stewart ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 1052e854dd38SRandall Stewart (tp->t_fbyte_out == 0) && 1053e854dd38SRandall Stewart (so->so_snd.sb_ccc > 0)) { 1054e854dd38SRandall Stewart tp->t_fbyte_out = ticks; 1055e854dd38SRandall Stewart if (tp->t_fbyte_out == 0) 1056e854dd38SRandall Stewart tp->t_fbyte_out = 1; 1057e854dd38SRandall Stewart if (tp->t_fbyte_out && tp->t_fbyte_in) 1058e854dd38SRandall Stewart tp->t_flags2 |= TF2_FBYTES_COMPLETE; 1059e854dd38SRandall Stewart } 10602cbcd3c1SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED) && 10612cbcd3c1SGleb Smirnoff !(flags & PRUS_NOTREADY)) { 1062b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 1063b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 1064f64dc2abSGleb Smirnoff error = tcp_output_nodrop(tp); 1065b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 1066b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 1067b0acefa8SBill Fenner } 10682c37256eSGarrett Wollman } else { 1069623dce13SRobert Watson /* 1070623dce13SRobert Watson * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 1071623dce13SRobert Watson */ 1072d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 10732c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 1074d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 10752c37256eSGarrett Wollman error = ENOBUFS; 10762c37256eSGarrett Wollman goto out; 10772c37256eSGarrett Wollman } 10782c37256eSGarrett Wollman /* 10792c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 10802c37256eSGarrett Wollman * the urgent pointer points to the last octet 10812c37256eSGarrett Wollman * of urgent data. We continue, however, 10822c37256eSGarrett Wollman * to consider it to indicate the first octet 10832c37256eSGarrett Wollman * of data past the urgent section. 10842c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 10852c37256eSGarrett Wollman */ 108608af8aacSRandall Stewart if (tp->t_acktime == 0) 108708af8aacSRandall Stewart tp->t_acktime = ticks; 1088651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_snd, m, flags); 1089d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 10907d2608a5SMark Johnston m = NULL; 1091ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 1092ef53690bSGarrett Wollman /* 1093ef53690bSGarrett Wollman * Do implied connect if not yet connected, 1094ef53690bSGarrett Wollman * initialize window to default value, and 10950c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 1096ef53690bSGarrett Wollman */ 109718a75309SPatrick Kelsey 1098c560df6fSPatrick Kelsey /* 1099c560df6fSPatrick Kelsey * Not going to contemplate SYN|URG 1100c560df6fSPatrick Kelsey */ 1101c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) 1102c560df6fSPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 1103fb59c426SYoshinobu Inoue #ifdef INET6 1104fb59c426SYoshinobu Inoue if (isipv6) 1105a9d22cceSGleb Smirnoff error = tcp6_connect(tp, sin6, td); 1106fb59c426SYoshinobu Inoue #endif /* INET6 */ 1107b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1108b287c6c7SBjoern A. Zeeb else 1109b287c6c7SBjoern A. Zeeb #endif 1110b287c6c7SBjoern A. Zeeb #ifdef INET 1111a9d22cceSGleb Smirnoff error = tcp_connect(tp, sinp, td); 1112b287c6c7SBjoern A. Zeeb #endif 11134a91aa8fSMichael Tuexen /* 11144a91aa8fSMichael Tuexen * The bind operation in tcp_connect succeeded. We 11154a91aa8fSMichael Tuexen * no longer want to restore the flags if later 11164a91aa8fSMichael Tuexen * operations fail. 11174a91aa8fSMichael Tuexen */ 11184a91aa8fSMichael Tuexen if (error == 0 || inp->inp_lport != 0) 11194a91aa8fSMichael Tuexen restoreflags = false; 11204a91aa8fSMichael Tuexen 11217d2608a5SMark Johnston if (error != 0) { 11227d2608a5SMark Johnston /* m is freed if PRUS_NOTREADY is unset. */ 11237d2608a5SMark Johnston sbflush(&so->so_snd); 1124ef53690bSGarrett Wollman goto out; 11257d2608a5SMark Johnston } 1126ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 1127ef53690bSGarrett Wollman tcp_mss(tp, -1); 1128623dce13SRobert Watson } 1129300fa232SGleb Smirnoff tp->snd_up = tp->snd_una + sbavail(&so->so_snd); 11307d2608a5SMark Johnston if ((flags & PRUS_NOTREADY) == 0) { 11312cdbfa66SPaul Saab tp->t_flags |= TF_FORCEDATA; 1132f64dc2abSGleb Smirnoff error = tcp_output_nodrop(tp); 11332cdbfa66SPaul Saab tp->t_flags &= ~TF_FORCEDATA; 11342c37256eSGarrett Wollman } 11352cbcd3c1SGleb Smirnoff } 11362529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, NULL, 11372529f56eSJonathan T. Looney &inp->inp_socket->so_rcv, 11382529f56eSJonathan T. Looney &inp->inp_socket->so_snd, 11392529f56eSJonathan T. Looney TCP_LOG_USERSEND, error, 11402529f56eSJonathan T. Looney 0, NULL, false); 11417d2608a5SMark Johnston 1142d1401c90SRobert Watson out: 11434a91aa8fSMichael Tuexen /* 11447d2608a5SMark Johnston * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is 11457d2608a5SMark Johnston * responsible for freeing memory. 11467d2608a5SMark Johnston */ 11477d2608a5SMark Johnston if (m != NULL && (flags & PRUS_NOTREADY) == 0) 11487d2608a5SMark Johnston m_freem(m); 11497d2608a5SMark Johnston 11507d2608a5SMark Johnston /* 11514a91aa8fSMichael Tuexen * If the request was unsuccessful and we changed flags, 11524a91aa8fSMichael Tuexen * restore the original flags. 11534a91aa8fSMichael Tuexen */ 11544a91aa8fSMichael Tuexen if (error != 0 && restoreflags) { 11554a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 11564a91aa8fSMichael Tuexen inp->inp_inc.inc_flags = incflagsav; 11574a91aa8fSMichael Tuexen } 11585d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB : 11595d06879aSGeorge V. Neville-Neil ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 1160f64dc2abSGleb Smirnoff error = tcp_unlock_or_drop(tp, error); 116197a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 116273fddedaSPeter Grehan return (error); 11632c37256eSGarrett Wollman } 11642c37256eSGarrett Wollman 11652cbcd3c1SGleb Smirnoff static int 11662cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count) 11672cbcd3c1SGleb Smirnoff { 1168109eb549SGleb Smirnoff struct epoch_tracker et; 11692cbcd3c1SGleb Smirnoff struct inpcb *inp; 11702cbcd3c1SGleb Smirnoff struct tcpcb *tp; 11712cbcd3c1SGleb Smirnoff int error; 11722cbcd3c1SGleb Smirnoff 11732cbcd3c1SGleb Smirnoff inp = sotoinpcb(so); 11742cbcd3c1SGleb Smirnoff INP_WLOCK(inp); 117553af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 11762cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 117782334850SJohn Baldwin mb_free_notready(m, count); 11782cbcd3c1SGleb Smirnoff return (ECONNRESET); 11792cbcd3c1SGleb Smirnoff } 11802cbcd3c1SGleb Smirnoff tp = intotcpcb(inp); 11812cbcd3c1SGleb Smirnoff 11822cbcd3c1SGleb Smirnoff SOCKBUF_LOCK(&so->so_snd); 11832cbcd3c1SGleb Smirnoff error = sbready(&so->so_snd, m, count); 11842cbcd3c1SGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 1185f64dc2abSGleb Smirnoff if (error) { 11862cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 1187f64dc2abSGleb Smirnoff return (error); 1188f64dc2abSGleb Smirnoff } 1189f64dc2abSGleb Smirnoff NET_EPOCH_ENTER(et); 1190f64dc2abSGleb Smirnoff error = tcp_output_unlock(tp); 1191f64dc2abSGleb Smirnoff NET_EPOCH_EXIT(et); 11922cbcd3c1SGleb Smirnoff 11932cbcd3c1SGleb Smirnoff return (error); 11942cbcd3c1SGleb Smirnoff } 11952cbcd3c1SGleb Smirnoff 11962c37256eSGarrett Wollman /* 1197a152f8a3SRobert Watson * Abort the TCP. Drop the connection abruptly. 11982c37256eSGarrett Wollman */ 1199ac45e92fSRobert Watson static void 12002c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 12012c37256eSGarrett Wollman { 1202f76fcf6dSJeffrey Hsu struct inpcb *inp; 1203a152f8a3SRobert Watson struct tcpcb *tp = NULL; 12046573d758SMatt Macy struct epoch_tracker et; 1205c78cbc7bSRobert Watson 1206ac45e92fSRobert Watson inp = sotoinpcb(so); 1207c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 1208c78cbc7bSRobert Watson 120997a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 12108501a69cSRobert Watson INP_WLOCK(inp); 1211c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 1212c78cbc7bSRobert Watson ("tcp_usr_abort: inp_socket == NULL")); 1213c78cbc7bSRobert Watson 1214c78cbc7bSRobert Watson /* 1215a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, drop. 1216c78cbc7bSRobert Watson */ 121753af6903SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED)) { 1218c78cbc7bSRobert Watson tp = intotcpcb(inp); 12198fa799bdSJonathan T. Looney tp = tcp_drop(tp, ECONNABORTED); 12208fa799bdSJonathan T. Looney if (tp == NULL) 12218fa799bdSJonathan T. Looney goto dropped; 12225d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ABORT); 1223c78cbc7bSRobert Watson } 1224ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1225d8596171SGleb Smirnoff soref(so); 1226ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1227a152f8a3SRobert Watson } 12288501a69cSRobert Watson INP_WUNLOCK(inp); 12298fa799bdSJonathan T. Looney dropped: 123097a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 1231a152f8a3SRobert Watson } 1232a152f8a3SRobert Watson 1233a152f8a3SRobert Watson /* 1234a152f8a3SRobert Watson * TCP socket is closed. Start friendly disconnect. 1235a152f8a3SRobert Watson */ 1236a152f8a3SRobert Watson static void 1237a152f8a3SRobert Watson tcp_usr_close(struct socket *so) 1238a152f8a3SRobert Watson { 1239a152f8a3SRobert Watson struct inpcb *inp; 1240a152f8a3SRobert Watson struct tcpcb *tp = NULL; 12416573d758SMatt Macy struct epoch_tracker et; 1242a152f8a3SRobert Watson 1243a152f8a3SRobert Watson inp = sotoinpcb(so); 1244a152f8a3SRobert Watson KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 1245a152f8a3SRobert Watson 124697a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 12478501a69cSRobert Watson INP_WLOCK(inp); 1248a152f8a3SRobert Watson KASSERT(inp->inp_socket != NULL, 1249a152f8a3SRobert Watson ("tcp_usr_close: inp_socket == NULL")); 1250a152f8a3SRobert Watson 1251a152f8a3SRobert Watson /* 1252a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, initiate 1253a152f8a3SRobert Watson * a disconnect. 1254a152f8a3SRobert Watson */ 125553af6903SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED)) { 1256a152f8a3SRobert Watson tp = intotcpcb(inp); 125774703901SGleb Smirnoff tp->t_flags |= TF_CLOSED; 1258a152f8a3SRobert Watson tcp_disconnect(tp); 12595d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CLOSE); 1260a152f8a3SRobert Watson } 1261ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1262d8596171SGleb Smirnoff soref(so); 1263ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1264a152f8a3SRobert Watson } 12658501a69cSRobert Watson INP_WUNLOCK(inp); 126697a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 12672c37256eSGarrett Wollman } 12682c37256eSGarrett Wollman 1269d3b6c96bSRandall Stewart static int 1270d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags) 1271d3b6c96bSRandall Stewart { 1272d3b6c96bSRandall Stewart /* 1273d3b6c96bSRandall Stewart * If the specific TCP stack has a pru_options 1274d3b6c96bSRandall Stewart * specified then it does not always support 1275d3b6c96bSRandall Stewart * all the PRU_XX options and we must ask it. 1276d3b6c96bSRandall Stewart * If the function is not specified then all 1277d3b6c96bSRandall Stewart * of the PRU_XX options are supported. 1278d3b6c96bSRandall Stewart */ 1279d3b6c96bSRandall Stewart int ret = 0; 1280d3b6c96bSRandall Stewart 1281d3b6c96bSRandall Stewart if (tp->t_fb->tfb_pru_options) { 1282d3b6c96bSRandall Stewart ret = (*tp->t_fb->tfb_pru_options)(tp, flags); 1283d3b6c96bSRandall Stewart } 1284d3b6c96bSRandall Stewart return (ret); 1285d3b6c96bSRandall Stewart } 1286d3b6c96bSRandall Stewart 12872c37256eSGarrett Wollman /* 12882c37256eSGarrett Wollman * Receive out-of-band data. 12892c37256eSGarrett Wollman */ 12902c37256eSGarrett Wollman static int 12912c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 12922c37256eSGarrett Wollman { 12932c37256eSGarrett Wollman int error = 0; 1294f76fcf6dSJeffrey Hsu struct inpcb *inp; 1295623dce13SRobert Watson struct tcpcb *tp = NULL; 12962c37256eSGarrett Wollman 1297623dce13SRobert Watson inp = sotoinpcb(so); 1298623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 12998501a69cSRobert Watson INP_WLOCK(inp); 130053af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 130121367f63SSam Leffler error = ECONNRESET; 1302623dce13SRobert Watson goto out; 1303623dce13SRobert Watson } 1304623dce13SRobert Watson tp = intotcpcb(inp); 1305d3b6c96bSRandall Stewart error = tcp_pru_options_support(tp, PRUS_OOB); 1306d3b6c96bSRandall Stewart if (error) { 1307d3b6c96bSRandall Stewart goto out; 1308d3b6c96bSRandall Stewart } 13092c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 1310c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 13114cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 13124cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 13132c37256eSGarrett Wollman error = EINVAL; 13142c37256eSGarrett Wollman goto out; 13152c37256eSGarrett Wollman } 13162c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 13172c37256eSGarrett Wollman error = EWOULDBLOCK; 13182c37256eSGarrett Wollman goto out; 13192c37256eSGarrett Wollman } 13202c37256eSGarrett Wollman m->m_len = 1; 13212c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 13222c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 13232c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1324623dce13SRobert Watson 1325623dce13SRobert Watson out: 13265d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVOOB); 13278501a69cSRobert Watson INP_WUNLOCK(inp); 1328623dce13SRobert Watson return (error); 13292c37256eSGarrett Wollman } 13302c37256eSGarrett Wollman 1331b287c6c7SBjoern A. Zeeb #ifdef INET 1332e7d02be1SGleb Smirnoff struct protosw tcp_protosw = { 1333e7d02be1SGleb Smirnoff .pr_type = SOCK_STREAM, 1334e7d02be1SGleb Smirnoff .pr_protocol = IPPROTO_TCP, 1335e7d02be1SGleb Smirnoff .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD | 1336e7d02be1SGleb Smirnoff PR_CAPATTACH, 1337e7d02be1SGleb Smirnoff .pr_ctloutput = tcp_ctloutput, 1338e7d02be1SGleb Smirnoff .pr_abort = tcp_usr_abort, 1339e7d02be1SGleb Smirnoff .pr_accept = tcp_usr_accept, 1340e7d02be1SGleb Smirnoff .pr_attach = tcp_usr_attach, 1341e7d02be1SGleb Smirnoff .pr_bind = tcp_usr_bind, 1342e7d02be1SGleb Smirnoff .pr_connect = tcp_usr_connect, 1343e7d02be1SGleb Smirnoff .pr_control = in_control, 1344e7d02be1SGleb Smirnoff .pr_detach = tcp_usr_detach, 1345e7d02be1SGleb Smirnoff .pr_disconnect = tcp_usr_disconnect, 1346e7d02be1SGleb Smirnoff .pr_listen = tcp_usr_listen, 1347e7d02be1SGleb Smirnoff .pr_peeraddr = in_getpeeraddr, 1348e7d02be1SGleb Smirnoff .pr_rcvd = tcp_usr_rcvd, 1349e7d02be1SGleb Smirnoff .pr_rcvoob = tcp_usr_rcvoob, 1350e7d02be1SGleb Smirnoff .pr_send = tcp_usr_send, 1351e7d02be1SGleb Smirnoff .pr_ready = tcp_usr_ready, 1352e7d02be1SGleb Smirnoff .pr_shutdown = tcp_usr_shutdown, 1353e7d02be1SGleb Smirnoff .pr_sockaddr = in_getsockaddr, 1354e7d02be1SGleb Smirnoff .pr_sosetlabel = in_pcbsosetlabel, 1355e7d02be1SGleb Smirnoff .pr_close = tcp_usr_close, 13562c37256eSGarrett Wollman }; 1357b287c6c7SBjoern A. Zeeb #endif /* INET */ 1358df8bae1dSRodney W. Grimes 1359fb59c426SYoshinobu Inoue #ifdef INET6 1360e7d02be1SGleb Smirnoff struct protosw tcp6_protosw = { 1361e7d02be1SGleb Smirnoff .pr_type = SOCK_STREAM, 1362e7d02be1SGleb Smirnoff .pr_protocol = IPPROTO_TCP, 1363e7d02be1SGleb Smirnoff .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD | 1364e7d02be1SGleb Smirnoff PR_CAPATTACH, 1365e7d02be1SGleb Smirnoff .pr_ctloutput = tcp_ctloutput, 1366e7d02be1SGleb Smirnoff .pr_abort = tcp_usr_abort, 1367e7d02be1SGleb Smirnoff .pr_accept = tcp6_usr_accept, 1368e7d02be1SGleb Smirnoff .pr_attach = tcp_usr_attach, 1369e7d02be1SGleb Smirnoff .pr_bind = tcp6_usr_bind, 1370e7d02be1SGleb Smirnoff .pr_connect = tcp6_usr_connect, 1371e7d02be1SGleb Smirnoff .pr_control = in6_control, 1372e7d02be1SGleb Smirnoff .pr_detach = tcp_usr_detach, 1373e7d02be1SGleb Smirnoff .pr_disconnect = tcp_usr_disconnect, 1374e7d02be1SGleb Smirnoff .pr_listen = tcp6_usr_listen, 1375e7d02be1SGleb Smirnoff .pr_peeraddr = in6_mapped_peeraddr, 1376e7d02be1SGleb Smirnoff .pr_rcvd = tcp_usr_rcvd, 1377e7d02be1SGleb Smirnoff .pr_rcvoob = tcp_usr_rcvoob, 1378e7d02be1SGleb Smirnoff .pr_send = tcp_usr_send, 1379e7d02be1SGleb Smirnoff .pr_ready = tcp_usr_ready, 1380e7d02be1SGleb Smirnoff .pr_shutdown = tcp_usr_shutdown, 1381e7d02be1SGleb Smirnoff .pr_sockaddr = in6_mapped_sockaddr, 1382e7d02be1SGleb Smirnoff .pr_sosetlabel = in_pcbsosetlabel, 1383e7d02be1SGleb Smirnoff .pr_close = tcp_usr_close, 1384fb59c426SYoshinobu Inoue }; 1385fb59c426SYoshinobu Inoue #endif /* INET6 */ 1386fb59c426SYoshinobu Inoue 1387b287c6c7SBjoern A. Zeeb #ifdef INET 1388a0292f23SGarrett Wollman /* 1389a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 1390dfc4d218SGleb Smirnoff * by struct sockaddr_in. Call in_pcbconnect() to choose local host address 1391dfc4d218SGleb Smirnoff * and assign a local port number and install the inpcb into the hash. 1392dfc4d218SGleb Smirnoff * Initialize connection parameters and enter SYN-SENT state. 1393a0292f23SGarrett Wollman */ 13940312fbe9SPoul-Henning Kamp static int 1395a9d22cceSGleb Smirnoff tcp_connect(struct tcpcb *tp, struct sockaddr_in *sin, struct thread *td) 1396a0292f23SGarrett Wollman { 13979e46ff4dSGleb Smirnoff struct inpcb *inp = tptoinpcb(tp); 13989eb0e832SGleb Smirnoff struct socket *so = tptosocket(tp); 1399c3229e05SDavid Greenman int error; 1400a0292f23SGarrett Wollman 1401c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 14028501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 140376f1499fSGleb Smirnoff 1404fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1405dfc4d218SGleb Smirnoff error = in_pcbconnect(inp, sin, td->td_ucred, true); 14069e46ff4dSGleb Smirnoff INP_HASH_WUNLOCK(&V_tcbinfo); 1407dfc4d218SGleb Smirnoff if (error != 0) 14089e46ff4dSGleb Smirnoff return (error); 1409a0292f23SGarrett Wollman 1410087b55eaSAndre Oppermann /* 1411087b55eaSAndre Oppermann * Compute window scaling to request: 1412087b55eaSAndre Oppermann * Scale to fit into sweet spot. See tcp_syncache.c. 1413087b55eaSAndre Oppermann * XXX: This should move to tcp_output(). 1414087b55eaSAndre Oppermann */ 1415a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 14169b3bc6bfSMike Silbersack (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1417a0292f23SGarrett Wollman tp->request_r_scale++; 1418a0292f23SGarrett Wollman 1419a0292f23SGarrett Wollman soisconnecting(so); 142078b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 142157f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 14228e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 14238e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 14248e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1425a0292f23SGarrett Wollman tcp_sendseqinit(tp); 1426a45d2726SAndras Olah 14279e46ff4dSGleb Smirnoff return (0); 1428a0292f23SGarrett Wollman } 1429b287c6c7SBjoern A. Zeeb #endif /* INET */ 1430a0292f23SGarrett Wollman 1431fb59c426SYoshinobu Inoue #ifdef INET6 1432fb59c426SYoshinobu Inoue static int 1433a9d22cceSGleb Smirnoff tcp6_connect(struct tcpcb *tp, struct sockaddr_in6 *sin6, struct thread *td) 1434fb59c426SYoshinobu Inoue { 14359eb0e832SGleb Smirnoff struct inpcb *inp = tptoinpcb(tp); 1436fb59c426SYoshinobu Inoue int error; 1437fb59c426SYoshinobu Inoue 1438*775da7f8SMark Johnston NET_EPOCH_ASSERT(); 14398501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1440623dce13SRobert Watson 144176f1499fSGleb Smirnoff INP_HASH_WLOCK(&V_tcbinfo); 1442a9d22cceSGleb Smirnoff error = in6_pcbconnect(inp, sin6, td->td_ucred, true); 1443fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 144476f1499fSGleb Smirnoff if (error != 0) 144576f1499fSGleb Smirnoff return (error); 1446fb59c426SYoshinobu Inoue 1447fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1448fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1449970caf60SBjoern A. Zeeb (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1450fb59c426SYoshinobu Inoue tp->request_r_scale++; 1451fb59c426SYoshinobu Inoue 1452a7e201bbSAndrey V. Elsukov soisconnecting(inp->inp_socket); 145378b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 145457f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 14558e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 14568e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 14578e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1458fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1459fb59c426SYoshinobu Inoue 146076f1499fSGleb Smirnoff return (0); 1461fb59c426SYoshinobu Inoue } 1462fb59c426SYoshinobu Inoue #endif /* INET6 */ 1463fb59c426SYoshinobu Inoue 1464cfe8b629SGarrett Wollman /* 1465b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1466b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1467b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1468b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1469b8af5dfaSRobert Watson * from Linux. 1470b8af5dfaSRobert Watson */ 1471b8af5dfaSRobert Watson static void 1472ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1473b8af5dfaSRobert Watson { 1474b8af5dfaSRobert Watson 14759eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 1476b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1477b8af5dfaSRobert Watson 1478b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1479b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1480b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 14813529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 1482b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1483b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1484b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1485b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1486b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1487b8af5dfaSRobert Watson } 14883f169c54SRichard Scheffenegger if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) 14895a17b6adSMichael Tuexen ti->tcpi_options |= TCPI_OPT_ECN; 14901baaf834SBruce M Simpson 149143d94734SJohn Baldwin ti->tcpi_rto = tp->t_rxtcur * tick; 14923ac12506SJonathan T. Looney ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick; 14931baaf834SBruce M Simpson ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 14941baaf834SBruce M Simpson ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 14951baaf834SBruce M Simpson 1496b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1497b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1498b8af5dfaSRobert Watson 1499b8af5dfaSRobert Watson /* 1500b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1501b8af5dfaSRobert Watson */ 1502c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1503535fbad6SKip Macy ti->tcpi_rcv_nxt = tp->rcv_nxt; 1504b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 15051c18314dSAndre Oppermann ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1506535fbad6SKip Macy ti->tcpi_snd_nxt = tp->snd_nxt; 150743d94734SJohn Baldwin ti->tcpi_snd_mss = tp->t_maxseg; 150843d94734SJohn Baldwin ti->tcpi_rcv_mss = tp->t_maxseg; 1509f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1510f5d34df5SGeorge V. Neville-Neil ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1511f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1512a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD 1513a6456410SNavdeep Parhar if (tp->t_flags & TF_TOE) { 1514a6456410SNavdeep Parhar ti->tcpi_options |= TCPI_OPT_TOE; 1515a6456410SNavdeep Parhar tcp_offload_tcp_info(tp, ti); 1516a6456410SNavdeep Parhar } 1517a6456410SNavdeep Parhar #endif 151822c81cc5SRichard Scheffenegger /* 151922c81cc5SRichard Scheffenegger * AccECN related counters. 152022c81cc5SRichard Scheffenegger */ 152122c81cc5SRichard Scheffenegger if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) == 152222c81cc5SRichard Scheffenegger (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) 152322c81cc5SRichard Scheffenegger /* 152422c81cc5SRichard Scheffenegger * Internal counter starts at 5 for AccECN 152522c81cc5SRichard Scheffenegger * but 0 for RFC3168 ECN. 152622c81cc5SRichard Scheffenegger */ 152722c81cc5SRichard Scheffenegger ti->tcpi_delivered_ce = tp->t_scep - 5; 152822c81cc5SRichard Scheffenegger else 152922c81cc5SRichard Scheffenegger ti->tcpi_delivered_ce = tp->t_scep; 153022c81cc5SRichard Scheffenegger ti->tcpi_received_ce = tp->t_rcep; 1531b8af5dfaSRobert Watson } 1532b8af5dfaSRobert Watson 1533b8af5dfaSRobert Watson /* 15341e8f5ffaSRobert Watson * tcp_ctloutput() must drop the inpcb lock before performing copyin on 15351e8f5ffaSRobert Watson * socket option arguments. When it re-acquires the lock after the copy, it 15361e8f5ffaSRobert Watson * has to revalidate that the connection is still valid for the socket 15371e8f5ffaSRobert Watson * option. 1538cfe8b629SGarrett Wollman */ 1539bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \ 15408501a69cSRobert Watson INP_WLOCK(inp); \ 154153af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { \ 15428501a69cSRobert Watson INP_WUNLOCK(inp); \ 1543bac5bedfSConrad Meyer cleanup; \ 15441e8f5ffaSRobert Watson return (ECONNRESET); \ 15451e8f5ffaSRobert Watson } \ 15461e8f5ffaSRobert Watson tp = intotcpcb(inp); \ 15471e8f5ffaSRobert Watson } while(0) 1548bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */) 15491e8f5ffaSRobert Watson 1550fd7daa72SMichael Tuexen int 1551fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt) 1552df8bae1dSRodney W. Grimes { 1553fd7daa72SMichael Tuexen struct socket *so = inp->inp_socket; 1554fc4d53ccSGleb Smirnoff struct tcpcb *tp = intotcpcb(inp); 1555fc4d53ccSGleb Smirnoff int error = 0; 1556df8bae1dSRodney W. Grimes 1557fc4d53ccSGleb Smirnoff MPASS(sopt->sopt_dir == SOPT_SET); 15583b3c08c1SMichael Tuexen INP_WLOCK_ASSERT(inp); 155953af6903SGleb Smirnoff KASSERT((inp->inp_flags & INP_DROPPED) == 0, 1560fd7daa72SMichael Tuexen ("inp_flags == %x", inp->inp_flags)); 1561fd7daa72SMichael Tuexen KASSERT(so != NULL, ("inp_socket == NULL")); 1562fc4d53ccSGleb Smirnoff 1563cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 15643b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 1565fb59c426SYoshinobu Inoue #ifdef INET6 1566de156263SGleb Smirnoff if (inp->inp_vflag & INP_IPV6PROTO) 1567fd7daa72SMichael Tuexen error = ip6_ctloutput(so, sopt); 1568de156263SGleb Smirnoff #endif 1569de156263SGleb Smirnoff #if defined(INET6) && defined(INET) 1570de156263SGleb Smirnoff else 1571de156263SGleb Smirnoff #endif 1572de156263SGleb Smirnoff #ifdef INET 1573fd7daa72SMichael Tuexen error = ip_ctloutput(so, sopt); 1574de156263SGleb Smirnoff #endif 15755dff1c38SMichael Tuexen /* 1576de156263SGleb Smirnoff * When an IP-level socket option affects TCP, pass control 1577de156263SGleb Smirnoff * down to stack tfb_tcp_ctloutput, otherwise return what 1578de156263SGleb Smirnoff * IP level returned. 15795dff1c38SMichael Tuexen */ 1580de156263SGleb Smirnoff switch (sopt->sopt_level) { 1581de156263SGleb Smirnoff #ifdef INET6 1582de156263SGleb Smirnoff case IPPROTO_IPV6: 1583de156263SGleb Smirnoff if ((inp->inp_vflag & INP_IPV6PROTO) == 0) 1584de156263SGleb Smirnoff return (error); 1585de156263SGleb Smirnoff switch (sopt->sopt_name) { 1586de156263SGleb Smirnoff case IPV6_TCLASS: 1587de156263SGleb Smirnoff /* Notify tcp stacks that care (e.g. RACK). */ 1588de156263SGleb Smirnoff break; 1589de156263SGleb Smirnoff case IPV6_USE_MIN_MTU: 1590f581a26eSGleb Smirnoff /* Update t_maxseg accordingly. */ 1591f581a26eSGleb Smirnoff break; 1592de156263SGleb Smirnoff default: 1593de156263SGleb Smirnoff return (error); 15945dff1c38SMichael Tuexen } 1595de156263SGleb Smirnoff break; 1596b287c6c7SBjoern A. Zeeb #endif 1597b287c6c7SBjoern A. Zeeb #ifdef INET 1598de156263SGleb Smirnoff case IPPROTO_IP: 1599de156263SGleb Smirnoff switch (sopt->sopt_name) { 1600de156263SGleb Smirnoff case IP_TOS: 16013b0ee680SRichard Scheffenegger inp->inp_ip_tos &= ~IPTOS_ECN_MASK; 16023b0ee680SRichard Scheffenegger break; 1603de156263SGleb Smirnoff case IP_TTL: 1604de156263SGleb Smirnoff /* Notify tcp stacks that care (e.g. RACK). */ 1605de156263SGleb Smirnoff break; 1606de156263SGleb Smirnoff default: 1607df8bae1dSRodney W. Grimes return (error); 1608de156263SGleb Smirnoff } 1609de156263SGleb Smirnoff break; 1610de156263SGleb Smirnoff #endif 1611de156263SGleb Smirnoff default: 1612de156263SGleb Smirnoff return (error); 1613de156263SGleb Smirnoff } 16143b3c08c1SMichael Tuexen INP_WLOCK(inp); 161553af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 16163b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 16173b3c08c1SMichael Tuexen return (ECONNRESET); 16183b3c08c1SMichael Tuexen } 1619fc4d53ccSGleb Smirnoff } else if (sopt->sopt_name == TCP_FUNCTION_BLK) { 1620fc4d53ccSGleb Smirnoff /* 1621fc4d53ccSGleb Smirnoff * Protect the TCP option TCP_FUNCTION_BLK so 1622fc4d53ccSGleb Smirnoff * that a sub-function can *never* overwrite this. 1623fc4d53ccSGleb Smirnoff */ 1624fc4d53ccSGleb Smirnoff struct tcp_function_set fsn; 1625fc4d53ccSGleb Smirnoff struct tcp_function_block *blk; 1626fc4d53ccSGleb Smirnoff 16273b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 1628fc4d53ccSGleb Smirnoff error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn); 1629fc4d53ccSGleb Smirnoff if (error) 1630fc4d53ccSGleb Smirnoff return (error); 1631fc4d53ccSGleb Smirnoff 163268cea2b1SJohn Baldwin INP_WLOCK(inp); 163353af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 16348501a69cSRobert Watson INP_WUNLOCK(inp); 16351e8f5ffaSRobert Watson return (ECONNRESET); 1636623dce13SRobert Watson } 163755bceb1eSRandall Stewart tp = intotcpcb(inp); 1638fc4d53ccSGleb Smirnoff 163955bceb1eSRandall Stewart blk = find_and_ref_tcp_functions(&fsn); 164055bceb1eSRandall Stewart if (blk == NULL) { 164155bceb1eSRandall Stewart INP_WUNLOCK(inp); 164255bceb1eSRandall Stewart return (ENOENT); 164355bceb1eSRandall Stewart } 1644587d67c0SRandall Stewart if (tp->t_fb == blk) { 1645587d67c0SRandall Stewart /* You already have this */ 1646587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1647587d67c0SRandall Stewart INP_WUNLOCK(inp); 1648587d67c0SRandall Stewart return (0); 1649587d67c0SRandall Stewart } 1650587d67c0SRandall Stewart if (tp->t_state != TCPS_CLOSED) { 1651587d67c0SRandall Stewart /* 1652587d67c0SRandall Stewart * The user has advanced the state 1653587d67c0SRandall Stewart * past the initial point, we may not 1654587d67c0SRandall Stewart * be able to switch. 1655587d67c0SRandall Stewart */ 1656587d67c0SRandall Stewart if (blk->tfb_tcp_handoff_ok != NULL) { 1657587d67c0SRandall Stewart /* 1658587d67c0SRandall Stewart * Does the stack provide a 1659587d67c0SRandall Stewart * query mechanism, if so it may 1660587d67c0SRandall Stewart * still be possible? 1661587d67c0SRandall Stewart */ 1662587d67c0SRandall Stewart error = (*blk->tfb_tcp_handoff_ok)(tp); 1663c6c0be27SMichael Tuexen } else 1664c6c0be27SMichael Tuexen error = EINVAL; 1665587d67c0SRandall Stewart if (error) { 1666587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1667587d67c0SRandall Stewart INP_WUNLOCK(inp); 1668587d67c0SRandall Stewart return(error); 1669587d67c0SRandall Stewart } 1670587d67c0SRandall Stewart } 167155bceb1eSRandall Stewart if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) { 167255bceb1eSRandall Stewart refcount_release(&blk->tfb_refcnt); 167355bceb1eSRandall Stewart INP_WUNLOCK(inp); 167455bceb1eSRandall Stewart return (ENOENT); 167555bceb1eSRandall Stewart } 167655bceb1eSRandall Stewart /* 167755bceb1eSRandall Stewart * Release the old refcnt, the 1678587d67c0SRandall Stewart * lookup acquired a ref on the 1679587d67c0SRandall Stewart * new one already. 168055bceb1eSRandall Stewart */ 1681587d67c0SRandall Stewart if (tp->t_fb->tfb_tcp_fb_fini) { 1682086a3556SAndrew Gallatin struct epoch_tracker et; 1683587d67c0SRandall Stewart /* 1684587d67c0SRandall Stewart * Tell the stack to cleanup with 0 i.e. 1685587d67c0SRandall Stewart * the tcb is not going away. 1686587d67c0SRandall Stewart */ 1687086a3556SAndrew Gallatin NET_EPOCH_ENTER(et); 1688587d67c0SRandall Stewart (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0); 1689086a3556SAndrew Gallatin NET_EPOCH_EXIT(et); 1690587d67c0SRandall Stewart } 16913ee9c3c4SRandall Stewart #ifdef TCPHPTS 16923ee9c3c4SRandall Stewart /* Assure that we are not on any hpts */ 16939eb0e832SGleb Smirnoff tcp_hpts_remove(tptoinpcb(tp)); 16943ee9c3c4SRandall Stewart #endif 16953ee9c3c4SRandall Stewart if (blk->tfb_tcp_fb_init) { 16963ee9c3c4SRandall Stewart error = (*blk->tfb_tcp_fb_init)(tp); 16973ee9c3c4SRandall Stewart if (error) { 16983ee9c3c4SRandall Stewart refcount_release(&blk->tfb_refcnt); 16993ee9c3c4SRandall Stewart if (tp->t_fb->tfb_tcp_fb_init) { 17003ee9c3c4SRandall Stewart if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) { 17013ee9c3c4SRandall Stewart /* Fall back failed, drop the connection */ 17023ee9c3c4SRandall Stewart INP_WUNLOCK(inp); 1703fd7daa72SMichael Tuexen soabort(so); 17043ee9c3c4SRandall Stewart return (error); 17053ee9c3c4SRandall Stewart } 17063ee9c3c4SRandall Stewart } 17073ee9c3c4SRandall Stewart goto err_out; 17083ee9c3c4SRandall Stewart } 17093ee9c3c4SRandall Stewart } 171055bceb1eSRandall Stewart refcount_release(&tp->t_fb->tfb_refcnt); 171155bceb1eSRandall Stewart tp->t_fb = blk; 171255bceb1eSRandall Stewart #ifdef TCP_OFFLOAD 171355bceb1eSRandall Stewart if (tp->t_flags & TF_TOE) { 171455bceb1eSRandall Stewart tcp_offload_ctloutput(tp, sopt->sopt_dir, 171555bceb1eSRandall Stewart sopt->sopt_name); 171655bceb1eSRandall Stewart } 171755bceb1eSRandall Stewart #endif 17183ee9c3c4SRandall Stewart err_out: 171955bceb1eSRandall Stewart INP_WUNLOCK(inp); 172055bceb1eSRandall Stewart return (error); 1721fc4d53ccSGleb Smirnoff } 1722fc4d53ccSGleb Smirnoff 17233b3c08c1SMichael Tuexen /* Pass in the INP locked, callee must unlock it. */ 17243b3c08c1SMichael Tuexen return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt)); 1725fc4d53ccSGleb Smirnoff } 1726fc4d53ccSGleb Smirnoff 1727fc4d53ccSGleb Smirnoff static int 1728fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt) 1729fc4d53ccSGleb Smirnoff { 1730fd7daa72SMichael Tuexen struct socket *so = inp->inp_socket; 1731fd7daa72SMichael Tuexen struct tcpcb *tp = intotcpcb(inp); 1732fc4d53ccSGleb Smirnoff int error = 0; 1733fc4d53ccSGleb Smirnoff 1734fc4d53ccSGleb Smirnoff MPASS(sopt->sopt_dir == SOPT_GET); 17353b3c08c1SMichael Tuexen INP_WLOCK_ASSERT(inp); 173653af6903SGleb Smirnoff KASSERT((inp->inp_flags & INP_DROPPED) == 0, 1737fd7daa72SMichael Tuexen ("inp_flags == %x", inp->inp_flags)); 1738fd7daa72SMichael Tuexen KASSERT(so != NULL, ("inp_socket == NULL")); 1739fc4d53ccSGleb Smirnoff 1740fc4d53ccSGleb Smirnoff if (sopt->sopt_level != IPPROTO_TCP) { 17413b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 1742fc4d53ccSGleb Smirnoff #ifdef INET6 1743fc4d53ccSGleb Smirnoff if (inp->inp_vflag & INP_IPV6PROTO) 1744fd7daa72SMichael Tuexen error = ip6_ctloutput(so, sopt); 1745fc4d53ccSGleb Smirnoff #endif /* INET6 */ 1746fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET) 1747fc4d53ccSGleb Smirnoff else 1748fc4d53ccSGleb Smirnoff #endif 1749fc4d53ccSGleb Smirnoff #ifdef INET 1750fd7daa72SMichael Tuexen error = ip_ctloutput(so, sopt); 1751fc4d53ccSGleb Smirnoff #endif 1752fc4d53ccSGleb Smirnoff return (error); 1753fc4d53ccSGleb Smirnoff } 1754fc4d53ccSGleb Smirnoff if (((sopt->sopt_name == TCP_FUNCTION_BLK) || 1755e2833083SPeter Lei (sopt->sopt_name == TCP_FUNCTION_ALIAS))) { 1756fc4d53ccSGleb Smirnoff struct tcp_function_set fsn; 1757fc4d53ccSGleb Smirnoff 1758e2833083SPeter Lei if (sopt->sopt_name == TCP_FUNCTION_ALIAS) { 1759e2833083SPeter Lei memset(&fsn, 0, sizeof(fsn)); 1760e2833083SPeter Lei find_tcp_function_alias(tp->t_fb, &fsn); 1761e2833083SPeter Lei } else { 1762e2833083SPeter Lei strncpy(fsn.function_set_name, 1763e2833083SPeter Lei tp->t_fb->tfb_tcp_block_name, 1764c73b6f4dSEd Maste TCP_FUNCTION_NAME_LEN_MAX); 1765c73b6f4dSEd Maste fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0'; 1766e2833083SPeter Lei } 176755bceb1eSRandall Stewart fsn.pcbcnt = tp->t_fb->tfb_refcnt; 176855bceb1eSRandall Stewart INP_WUNLOCK(inp); 176955bceb1eSRandall Stewart error = sooptcopyout(sopt, &fsn, sizeof fsn); 177055bceb1eSRandall Stewart return (error); 177155bceb1eSRandall Stewart } 1772fc4d53ccSGleb Smirnoff 17733b3c08c1SMichael Tuexen /* Pass in the INP locked, callee must unlock it. */ 17743b3c08c1SMichael Tuexen return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt)); 1775fc4d53ccSGleb Smirnoff } 1776fc4d53ccSGleb Smirnoff 1777fc4d53ccSGleb Smirnoff int 1778fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1779fc4d53ccSGleb Smirnoff { 1780fc4d53ccSGleb Smirnoff struct inpcb *inp; 1781fc4d53ccSGleb Smirnoff 1782fc4d53ccSGleb Smirnoff inp = sotoinpcb(so); 1783fc4d53ccSGleb Smirnoff KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1784fc4d53ccSGleb Smirnoff 17853b3c08c1SMichael Tuexen INP_WLOCK(inp); 178653af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 17873b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 17883b3c08c1SMichael Tuexen return (ECONNRESET); 17893b3c08c1SMichael Tuexen } 1790fc4d53ccSGleb Smirnoff if (sopt->sopt_dir == SOPT_SET) 1791fc4d53ccSGleb Smirnoff return (tcp_ctloutput_set(inp, sopt)); 1792fc4d53ccSGleb Smirnoff else if (sopt->sopt_dir == SOPT_GET) 1793fc4d53ccSGleb Smirnoff return (tcp_ctloutput_get(inp, sopt)); 1794fc4d53ccSGleb Smirnoff else 1795fc4d53ccSGleb Smirnoff panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir); 179655bceb1eSRandall Stewart } 179755bceb1eSRandall Stewart 17982529f56eSJonathan T. Looney /* 17992529f56eSJonathan T. Looney * If this assert becomes untrue, we need to change the size of the buf 18002529f56eSJonathan T. Looney * variable in tcp_default_ctloutput(). 18012529f56eSJonathan T. Looney */ 18022529f56eSJonathan T. Looney #ifdef CTASSERT 18032529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN); 18042529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN); 18052529f56eSJonathan T. Looney #endif 18062529f56eSJonathan T. Looney 1807ec1db6e1SJohn Baldwin #ifdef KERN_TLS 1808ec1db6e1SJohn Baldwin static int 1809ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls) 1810ec1db6e1SJohn Baldwin { 1811ec1db6e1SJohn Baldwin struct tls_enable_v0 tls_v0; 1812ec1db6e1SJohn Baldwin int error; 1813ec1db6e1SJohn Baldwin 1814ec1db6e1SJohn Baldwin if (sopt->sopt_valsize == sizeof(tls_v0)) { 1815ec1db6e1SJohn Baldwin error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0), 1816ec1db6e1SJohn Baldwin sizeof(tls_v0)); 1817ec1db6e1SJohn Baldwin if (error) 1818ec1db6e1SJohn Baldwin return (error); 1819ec1db6e1SJohn Baldwin memset(tls, 0, sizeof(*tls)); 1820ec1db6e1SJohn Baldwin tls->cipher_key = tls_v0.cipher_key; 1821ec1db6e1SJohn Baldwin tls->iv = tls_v0.iv; 1822ec1db6e1SJohn Baldwin tls->auth_key = tls_v0.auth_key; 1823ec1db6e1SJohn Baldwin tls->cipher_algorithm = tls_v0.cipher_algorithm; 1824ec1db6e1SJohn Baldwin tls->cipher_key_len = tls_v0.cipher_key_len; 1825ec1db6e1SJohn Baldwin tls->iv_len = tls_v0.iv_len; 1826ec1db6e1SJohn Baldwin tls->auth_algorithm = tls_v0.auth_algorithm; 1827ec1db6e1SJohn Baldwin tls->auth_key_len = tls_v0.auth_key_len; 1828ec1db6e1SJohn Baldwin tls->flags = tls_v0.flags; 1829ec1db6e1SJohn Baldwin tls->tls_vmajor = tls_v0.tls_vmajor; 1830ec1db6e1SJohn Baldwin tls->tls_vminor = tls_v0.tls_vminor; 1831ec1db6e1SJohn Baldwin return (0); 1832ec1db6e1SJohn Baldwin } 1833ec1db6e1SJohn Baldwin 1834ec1db6e1SJohn Baldwin return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls))); 1835ec1db6e1SJohn Baldwin } 1836ec1db6e1SJohn Baldwin #endif 1837ec1db6e1SJohn Baldwin 1838b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo; 1839b8d60729SRandall Stewart 1840b8d60729SRandall Stewart static int 1841ea9017fbSRandall Stewart tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt) 1842b8d60729SRandall Stewart { 1843b8d60729SRandall Stewart struct cc_algo *algo; 1844b8d60729SRandall Stewart void *ptr = NULL; 18453b3c08c1SMichael Tuexen struct tcpcb *tp; 1846b8d60729SRandall Stewart struct cc_var cc_mem; 1847b8d60729SRandall Stewart char buf[TCP_CA_NAME_MAX]; 1848b8d60729SRandall Stewart size_t mem_sz; 1849b8d60729SRandall Stewart int error; 1850b8d60729SRandall Stewart 1851b8d60729SRandall Stewart INP_WUNLOCK(inp); 1852b8d60729SRandall Stewart error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1); 1853b8d60729SRandall Stewart if (error) 1854b8d60729SRandall Stewart return(error); 1855b8d60729SRandall Stewart buf[sopt->sopt_valsize] = '\0'; 1856b8d60729SRandall Stewart CC_LIST_RLOCK(); 1857ea9017fbSRandall Stewart STAILQ_FOREACH(algo, &cc_list, entries) { 1858b8d60729SRandall Stewart if (strncmp(buf, algo->name, 1859b8d60729SRandall Stewart TCP_CA_NAME_MAX) == 0) { 1860b8d60729SRandall Stewart if (algo->flags & CC_MODULE_BEING_REMOVED) { 1861b8d60729SRandall Stewart /* We can't "see" modules being unloaded */ 1862b8d60729SRandall Stewart continue; 1863b8d60729SRandall Stewart } 1864b8d60729SRandall Stewart break; 1865b8d60729SRandall Stewart } 1866ea9017fbSRandall Stewart } 1867b8d60729SRandall Stewart if (algo == NULL) { 1868b8d60729SRandall Stewart CC_LIST_RUNLOCK(); 1869b8d60729SRandall Stewart return(ESRCH); 1870b8d60729SRandall Stewart } 1871ea9017fbSRandall Stewart /* 1872ea9017fbSRandall Stewart * With a reference the algorithm cannot be removed 1873ea9017fbSRandall Stewart * so we hold a reference through the change process. 1874ea9017fbSRandall Stewart */ 1875ea9017fbSRandall Stewart cc_refer(algo); 1876ea9017fbSRandall Stewart CC_LIST_RUNLOCK(); 1877b8d60729SRandall Stewart if (algo->cb_init != NULL) { 1878b8d60729SRandall Stewart /* We can now pre-get the memory for the CC */ 1879b8d60729SRandall Stewart mem_sz = (*algo->cc_data_sz)(); 1880b8d60729SRandall Stewart if (mem_sz == 0) { 1881b8d60729SRandall Stewart goto no_mem_needed; 1882b8d60729SRandall Stewart } 1883b8d60729SRandall Stewart ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK); 1884b8d60729SRandall Stewart } else { 1885b8d60729SRandall Stewart no_mem_needed: 1886b8d60729SRandall Stewart mem_sz = 0; 1887b8d60729SRandall Stewart ptr = NULL; 1888b8d60729SRandall Stewart } 1889b8d60729SRandall Stewart /* 1890b8d60729SRandall Stewart * Make sure its all clean and zero and also get 1891b8d60729SRandall Stewart * back the inplock. 1892b8d60729SRandall Stewart */ 1893b8d60729SRandall Stewart memset(&cc_mem, 0, sizeof(cc_mem)); 1894df07bfdaSMichael Tuexen INP_WLOCK(inp); 189553af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 1896df07bfdaSMichael Tuexen INP_WUNLOCK(inp); 1897ea9017fbSRandall Stewart if (ptr) 1898df07bfdaSMichael Tuexen free(ptr, M_CC_MEM); 1899ea9017fbSRandall Stewart /* Release our temp reference */ 1900ea9017fbSRandall Stewart CC_LIST_RLOCK(); 1901ea9017fbSRandall Stewart cc_release(algo); 1902ea9017fbSRandall Stewart CC_LIST_RUNLOCK(); 1903df07bfdaSMichael Tuexen return (ECONNRESET); 1904df07bfdaSMichael Tuexen } 1905df07bfdaSMichael Tuexen tp = intotcpcb(inp); 1906df07bfdaSMichael Tuexen if (ptr != NULL) 1907b8d60729SRandall Stewart memset(ptr, 0, mem_sz); 1908b8d60729SRandall Stewart cc_mem.ccvc.tcp = tp; 1909b8d60729SRandall Stewart /* 1910b8d60729SRandall Stewart * We once again hold a write lock over the tcb so it's 1911b8d60729SRandall Stewart * safe to do these things without ordering concerns. 1912b8d60729SRandall Stewart * Note here we init into stack memory. 1913b8d60729SRandall Stewart */ 1914b8d60729SRandall Stewart if (algo->cb_init != NULL) 1915b8d60729SRandall Stewart error = algo->cb_init(&cc_mem, ptr); 1916b8d60729SRandall Stewart else 1917b8d60729SRandall Stewart error = 0; 1918b8d60729SRandall Stewart /* 1919b8d60729SRandall Stewart * The CC algorithms, when given their memory 1920b8d60729SRandall Stewart * should not fail we could in theory have a 1921b8d60729SRandall Stewart * KASSERT here. 1922b8d60729SRandall Stewart */ 1923b8d60729SRandall Stewart if (error == 0) { 1924b8d60729SRandall Stewart /* 1925b8d60729SRandall Stewart * Touchdown, lets go ahead and move the 1926b8d60729SRandall Stewart * connection to the new CC module by 1927b8d60729SRandall Stewart * copying in the cc_mem after we call 1928b8d60729SRandall Stewart * the old ones cleanup (if any). 1929b8d60729SRandall Stewart */ 1930b8d60729SRandall Stewart if (CC_ALGO(tp)->cb_destroy != NULL) 1931e68b3792SGleb Smirnoff CC_ALGO(tp)->cb_destroy(&tp->t_ccv); 1932ea9017fbSRandall Stewart /* Detach the old CC from the tcpcb */ 1933ea9017fbSRandall Stewart cc_detach(tp); 1934ea9017fbSRandall Stewart /* Copy in our temp memory that was inited */ 1935e68b3792SGleb Smirnoff memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var)); 1936ea9017fbSRandall Stewart /* Now attach the new, which takes a reference */ 1937ea9017fbSRandall Stewart cc_attach(tp, algo); 1938b8d60729SRandall Stewart /* Ok now are we where we have gotten past any conn_init? */ 1939b8d60729SRandall Stewart if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) { 1940b8d60729SRandall Stewart /* Yep run the connection init for the new CC */ 1941e68b3792SGleb Smirnoff CC_ALGO(tp)->conn_init(&tp->t_ccv); 1942b8d60729SRandall Stewart } 1943b8d60729SRandall Stewart } else if (ptr) 1944b8d60729SRandall Stewart free(ptr, M_CC_MEM); 1945b8d60729SRandall Stewart INP_WUNLOCK(inp); 1946ea9017fbSRandall Stewart /* Now lets release our temp reference */ 1947ea9017fbSRandall Stewart CC_LIST_RLOCK(); 1948ea9017fbSRandall Stewart cc_release(algo); 1949ea9017fbSRandall Stewart CC_LIST_RUNLOCK(); 1950b8d60729SRandall Stewart return (error); 1951b8d60729SRandall Stewart } 1952b8d60729SRandall Stewart 195355bceb1eSRandall Stewart int 19543b3c08c1SMichael Tuexen tcp_default_ctloutput(struct inpcb *inp, struct sockopt *sopt) 195555bceb1eSRandall Stewart { 1956fd7daa72SMichael Tuexen struct tcpcb *tp = intotcpcb(inp); 195755bceb1eSRandall Stewart int error, opt, optval; 195855bceb1eSRandall Stewart u_int ui; 195955bceb1eSRandall Stewart struct tcp_info ti; 1960b2e60773SJohn Baldwin #ifdef KERN_TLS 1961b2e60773SJohn Baldwin struct tls_enable tls; 1962528c7649SMichael Tuexen struct socket *so = inp->inp_socket; 1963b2e60773SJohn Baldwin #endif 19642529f56eSJonathan T. Looney char *pbuf, buf[TCP_LOG_ID_LEN]; 1965adc56f5aSEdward Tomasz Napierala #ifdef STATS 1966adc56f5aSEdward Tomasz Napierala struct statsblob *sbp; 1967adc56f5aSEdward Tomasz Napierala #endif 1968af6fef3aSGleb Smirnoff size_t len; 1969df8bae1dSRodney W. Grimes 1970f581a26eSGleb Smirnoff INP_WLOCK_ASSERT(inp); 197153af6903SGleb Smirnoff KASSERT((inp->inp_flags & INP_DROPPED) == 0, 1972fd7daa72SMichael Tuexen ("inp_flags == %x", inp->inp_flags)); 1973528c7649SMichael Tuexen KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL")); 1974f581a26eSGleb Smirnoff 1975f581a26eSGleb Smirnoff switch (sopt->sopt_level) { 1976f581a26eSGleb Smirnoff #ifdef INET6 1977f581a26eSGleb Smirnoff case IPPROTO_IPV6: 1978f581a26eSGleb Smirnoff MPASS(inp->inp_vflag & INP_IPV6PROTO); 1979f581a26eSGleb Smirnoff switch (sopt->sopt_name) { 1980f581a26eSGleb Smirnoff case IPV6_USE_MIN_MTU: 1981f581a26eSGleb Smirnoff tcp6_use_min_mtu(tp); 1982f581a26eSGleb Smirnoff /* FALLTHROUGH */ 1983f581a26eSGleb Smirnoff } 1984f581a26eSGleb Smirnoff INP_WUNLOCK(inp); 1985f581a26eSGleb Smirnoff return (0); 1986f581a26eSGleb Smirnoff #endif 1987f581a26eSGleb Smirnoff #ifdef INET 1988f581a26eSGleb Smirnoff case IPPROTO_IP: 1989f581a26eSGleb Smirnoff INP_WUNLOCK(inp); 1990f581a26eSGleb Smirnoff return (0); 1991f581a26eSGleb Smirnoff #endif 1992f581a26eSGleb Smirnoff } 1993f581a26eSGleb Smirnoff 1994d519cedbSGleb Smirnoff /* 1995d519cedbSGleb Smirnoff * For TCP_CCALGOOPT forward the control to CC module, for both 1996d519cedbSGleb Smirnoff * SOPT_SET and SOPT_GET. 1997d519cedbSGleb Smirnoff */ 1998d519cedbSGleb Smirnoff switch (sopt->sopt_name) { 1999d519cedbSGleb Smirnoff case TCP_CCALGOOPT: 2000d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 2001c8b53cedSMichael Tuexen if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT) 2002c8b53cedSMichael Tuexen return (EINVAL); 2003af6fef3aSGleb Smirnoff pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO); 2004af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize, 2005d519cedbSGleb Smirnoff sopt->sopt_valsize); 2006d519cedbSGleb Smirnoff if (error) { 2007af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 2008d519cedbSGleb Smirnoff return (error); 2009d519cedbSGleb Smirnoff } 2010bac5bedfSConrad Meyer INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP)); 2011d519cedbSGleb Smirnoff if (CC_ALGO(tp)->ctl_output != NULL) 2012e68b3792SGleb Smirnoff error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf); 2013d519cedbSGleb Smirnoff else 2014d519cedbSGleb Smirnoff error = ENOENT; 2015d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 2016d519cedbSGleb Smirnoff if (error == 0 && sopt->sopt_dir == SOPT_GET) 2017af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize); 2018af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 2019d519cedbSGleb Smirnoff return (error); 2020d519cedbSGleb Smirnoff } 2021d519cedbSGleb Smirnoff 2022cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 2023cfe8b629SGarrett Wollman case SOPT_SET: 2024cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2025fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 202688f6b043SBruce M Simpson case TCP_MD5SIG: 20278501a69cSRobert Watson INP_WUNLOCK(inp); 202897453e5eSClaudio Jeker if (!TCPMD5_ENABLED()) 2029fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 2030fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 20311cfd4b53SBruce M Simpson if (error) 20321e8f5ffaSRobert Watson return (error); 203397453e5eSClaudio Jeker INP_WLOCK_RECHECK(inp); 203409fe6320SNavdeep Parhar goto unlock_and_done; 2035fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 203609fe6320SNavdeep Parhar 2037df8bae1dSRodney W. Grimes case TCP_NODELAY: 2038cfe8b629SGarrett Wollman case TCP_NOOPT: 20390471a8c7SRichard Scheffenegger case TCP_LRD: 20408501a69cSRobert Watson INP_WUNLOCK(inp); 2041cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 2042cfe8b629SGarrett Wollman sizeof optval); 2043cfe8b629SGarrett Wollman if (error) 20441e8f5ffaSRobert Watson return (error); 2045cfe8b629SGarrett Wollman 20468501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 2047cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2048cfe8b629SGarrett Wollman case TCP_NODELAY: 2049cfe8b629SGarrett Wollman opt = TF_NODELAY; 2050cfe8b629SGarrett Wollman break; 2051cfe8b629SGarrett Wollman case TCP_NOOPT: 2052cfe8b629SGarrett Wollman opt = TF_NOOPT; 2053cfe8b629SGarrett Wollman break; 20540471a8c7SRichard Scheffenegger case TCP_LRD: 20550471a8c7SRichard Scheffenegger opt = TF_LRD; 20560471a8c7SRichard Scheffenegger break; 2057cfe8b629SGarrett Wollman default: 2058cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 2059cfe8b629SGarrett Wollman break; 2060cfe8b629SGarrett Wollman } 2061cfe8b629SGarrett Wollman 2062cfe8b629SGarrett Wollman if (optval) 2063cfe8b629SGarrett Wollman tp->t_flags |= opt; 2064df8bae1dSRodney W. Grimes else 2065cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 206609fe6320SNavdeep Parhar unlock_and_done: 206709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 206809fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 206909fe6320SNavdeep Parhar tcp_offload_ctloutput(tp, sopt->sopt_dir, 207009fe6320SNavdeep Parhar sopt->sopt_name); 207109fe6320SNavdeep Parhar } 207209fe6320SNavdeep Parhar #endif 20738501a69cSRobert Watson INP_WUNLOCK(inp); 2074df8bae1dSRodney W. Grimes break; 2075df8bae1dSRodney W. Grimes 2076007581c0SJonathan Lemon case TCP_NOPUSH: 20778501a69cSRobert Watson INP_WUNLOCK(inp); 2078007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 2079007581c0SJonathan Lemon sizeof optval); 2080007581c0SJonathan Lemon if (error) 20811e8f5ffaSRobert Watson return (error); 2082007581c0SJonathan Lemon 20838501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 2084007581c0SJonathan Lemon if (optval) 2085007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 2086d28b9e89SJohn Baldwin else if (tp->t_flags & TF_NOPUSH) { 2087007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 2088109eb549SGleb Smirnoff if (TCPS_HAVEESTABLISHED(tp->t_state)) { 2089109eb549SGleb Smirnoff struct epoch_tracker et; 2090109eb549SGleb Smirnoff 2091109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 2092f64dc2abSGleb Smirnoff error = tcp_output_nodrop(tp); 2093109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 2094109eb549SGleb Smirnoff } 2095007581c0SJonathan Lemon } 209609fe6320SNavdeep Parhar goto unlock_and_done; 2097007581c0SJonathan Lemon 20989e644c23SMichael Tuexen case TCP_REMOTE_UDP_ENCAPS_PORT: 20999e644c23SMichael Tuexen INP_WUNLOCK(inp); 21009e644c23SMichael Tuexen error = sooptcopyin(sopt, &optval, sizeof optval, 21019e644c23SMichael Tuexen sizeof optval); 21029e644c23SMichael Tuexen if (error) 21039e644c23SMichael Tuexen return (error); 21049e644c23SMichael Tuexen if ((optval < TCP_TUNNELING_PORT_MIN) || 21059e644c23SMichael Tuexen (optval > TCP_TUNNELING_PORT_MAX)) { 21069e644c23SMichael Tuexen /* Its got to be in range */ 21079e644c23SMichael Tuexen return (EINVAL); 21089e644c23SMichael Tuexen } 21099e644c23SMichael Tuexen if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) { 21109e644c23SMichael Tuexen /* You have to have enabled a UDP tunneling port first */ 21119e644c23SMichael Tuexen return (EINVAL); 21129e644c23SMichael Tuexen } 21139e644c23SMichael Tuexen INP_WLOCK_RECHECK(inp); 21149e644c23SMichael Tuexen if (tp->t_state != TCPS_CLOSED) { 21159e644c23SMichael Tuexen /* You can't change after you are connected */ 21169e644c23SMichael Tuexen error = EINVAL; 21179e644c23SMichael Tuexen } else { 21189e644c23SMichael Tuexen /* Ok we are all good set the port */ 21199e644c23SMichael Tuexen tp->t_port = htons(optval); 21209e644c23SMichael Tuexen } 21219e644c23SMichael Tuexen goto unlock_and_done; 21229e644c23SMichael Tuexen 2123df8bae1dSRodney W. Grimes case TCP_MAXSEG: 21248501a69cSRobert Watson INP_WUNLOCK(inp); 2125cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 2126cfe8b629SGarrett Wollman sizeof optval); 2127cfe8b629SGarrett Wollman if (error) 21281e8f5ffaSRobert Watson return (error); 2129df8bae1dSRodney W. Grimes 21308501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 213153369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 2132603724d3SBjoern A. Zeeb optval + 40 >= V_tcp_minmss) 2133cfe8b629SGarrett Wollman tp->t_maxseg = optval; 2134a0292f23SGarrett Wollman else 2135a0292f23SGarrett Wollman error = EINVAL; 213609fe6320SNavdeep Parhar goto unlock_and_done; 2137a0292f23SGarrett Wollman 2138b8af5dfaSRobert Watson case TCP_INFO: 21398501a69cSRobert Watson INP_WUNLOCK(inp); 2140b8af5dfaSRobert Watson error = EINVAL; 2141b8af5dfaSRobert Watson break; 2142b8af5dfaSRobert Watson 2143adc56f5aSEdward Tomasz Napierala case TCP_STATS: 2144adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2145adc56f5aSEdward Tomasz Napierala #ifdef STATS 2146adc56f5aSEdward Tomasz Napierala error = sooptcopyin(sopt, &optval, sizeof optval, 2147adc56f5aSEdward Tomasz Napierala sizeof optval); 2148adc56f5aSEdward Tomasz Napierala if (error) 2149adc56f5aSEdward Tomasz Napierala return (error); 2150adc56f5aSEdward Tomasz Napierala 2151adc56f5aSEdward Tomasz Napierala if (optval > 0) 2152adc56f5aSEdward Tomasz Napierala sbp = stats_blob_alloc( 2153adc56f5aSEdward Tomasz Napierala V_tcp_perconn_stats_dflt_tpl, 0); 2154adc56f5aSEdward Tomasz Napierala else 2155adc56f5aSEdward Tomasz Napierala sbp = NULL; 2156adc56f5aSEdward Tomasz Napierala 2157adc56f5aSEdward Tomasz Napierala INP_WLOCK_RECHECK(inp); 2158adc56f5aSEdward Tomasz Napierala if ((tp->t_stats != NULL && sbp == NULL) || 2159adc56f5aSEdward Tomasz Napierala (tp->t_stats == NULL && sbp != NULL)) { 2160adc56f5aSEdward Tomasz Napierala struct statsblob *t = tp->t_stats; 2161adc56f5aSEdward Tomasz Napierala tp->t_stats = sbp; 2162adc56f5aSEdward Tomasz Napierala sbp = t; 2163adc56f5aSEdward Tomasz Napierala } 2164adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2165adc56f5aSEdward Tomasz Napierala 2166adc56f5aSEdward Tomasz Napierala stats_blob_destroy(sbp); 2167adc56f5aSEdward Tomasz Napierala #else 2168adc56f5aSEdward Tomasz Napierala return (EOPNOTSUPP); 2169adc56f5aSEdward Tomasz Napierala #endif /* !STATS */ 2170adc56f5aSEdward Tomasz Napierala break; 2171adc56f5aSEdward Tomasz Napierala 2172dbc42409SLawrence Stewart case TCP_CONGESTION: 2173ea9017fbSRandall Stewart error = tcp_set_cc_mod(inp, sopt); 217473e263b1SGleb Smirnoff break; 2175dbc42409SLawrence Stewart 2176a034518aSAndrew Gallatin case TCP_REUSPORT_LB_NUMA: 2177a034518aSAndrew Gallatin INP_WUNLOCK(inp); 2178a034518aSAndrew Gallatin error = sooptcopyin(sopt, &optval, sizeof(optval), 2179a034518aSAndrew Gallatin sizeof(optval)); 2180a034518aSAndrew Gallatin INP_WLOCK_RECHECK(inp); 2181a034518aSAndrew Gallatin if (!error) 2182a034518aSAndrew Gallatin error = in_pcblbgroup_numa(inp, optval); 2183a034518aSAndrew Gallatin INP_WUNLOCK(inp); 2184a034518aSAndrew Gallatin break; 2185a034518aSAndrew Gallatin 2186b2e60773SJohn Baldwin #ifdef KERN_TLS 2187b2e60773SJohn Baldwin case TCP_TXTLS_ENABLE: 2188b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2189ec1db6e1SJohn Baldwin error = copyin_tls_enable(sopt, &tls); 2190b2e60773SJohn Baldwin if (error) 2191b2e60773SJohn Baldwin break; 2192fd7daa72SMichael Tuexen error = ktls_enable_tx(so, &tls); 2193b2e60773SJohn Baldwin break; 2194b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2195b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2196b2e60773SJohn Baldwin error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 2197b2e60773SJohn Baldwin if (error) 2198b2e60773SJohn Baldwin return (error); 2199b2e60773SJohn Baldwin 2200b2e60773SJohn Baldwin INP_WLOCK_RECHECK(inp); 2201fd7daa72SMichael Tuexen error = ktls_set_tx_mode(so, ui); 2202b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2203b2e60773SJohn Baldwin break; 2204f1f93475SJohn Baldwin case TCP_RXTLS_ENABLE: 2205f1f93475SJohn Baldwin INP_WUNLOCK(inp); 2206f1f93475SJohn Baldwin error = sooptcopyin(sopt, &tls, sizeof(tls), 2207f1f93475SJohn Baldwin sizeof(tls)); 2208f1f93475SJohn Baldwin if (error) 2209f1f93475SJohn Baldwin break; 2210fd7daa72SMichael Tuexen error = ktls_enable_rx(so, &tls); 2211f1f93475SJohn Baldwin break; 2212b2e60773SJohn Baldwin #endif 221308af8aacSRandall Stewart case TCP_MAXUNACKTIME: 22149077f387SGleb Smirnoff case TCP_KEEPIDLE: 22159077f387SGleb Smirnoff case TCP_KEEPINTVL: 22169077f387SGleb Smirnoff case TCP_KEEPINIT: 22179077f387SGleb Smirnoff INP_WUNLOCK(inp); 22189077f387SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 22199077f387SGleb Smirnoff if (error) 22209077f387SGleb Smirnoff return (error); 22219077f387SGleb Smirnoff 22229077f387SGleb Smirnoff if (ui > (UINT_MAX / hz)) { 22239077f387SGleb Smirnoff error = EINVAL; 22249077f387SGleb Smirnoff break; 22259077f387SGleb Smirnoff } 22269077f387SGleb Smirnoff ui *= hz; 22279077f387SGleb Smirnoff 22289077f387SGleb Smirnoff INP_WLOCK_RECHECK(inp); 22299077f387SGleb Smirnoff switch (sopt->sopt_name) { 223008af8aacSRandall Stewart case TCP_MAXUNACKTIME: 223108af8aacSRandall Stewart tp->t_maxunacktime = ui; 223208af8aacSRandall Stewart break; 223308af8aacSRandall Stewart 22349077f387SGleb Smirnoff case TCP_KEEPIDLE: 22359077f387SGleb Smirnoff tp->t_keepidle = ui; 22369077f387SGleb Smirnoff /* 22379077f387SGleb Smirnoff * XXX: better check current remaining 22389077f387SGleb Smirnoff * timeout and "merge" it with new value. 22399077f387SGleb Smirnoff */ 22409077f387SGleb Smirnoff if ((tp->t_state > TCPS_LISTEN) && 22419077f387SGleb Smirnoff (tp->t_state <= TCPS_CLOSING)) 22429077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 22439077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 22449077f387SGleb Smirnoff break; 22459077f387SGleb Smirnoff case TCP_KEEPINTVL: 22469077f387SGleb Smirnoff tp->t_keepintvl = ui; 22479077f387SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 22489077f387SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 22499077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 22509077f387SGleb Smirnoff TP_MAXIDLE(tp)); 22519077f387SGleb Smirnoff break; 22529077f387SGleb Smirnoff case TCP_KEEPINIT: 22539077f387SGleb Smirnoff tp->t_keepinit = ui; 22549077f387SGleb Smirnoff if (tp->t_state == TCPS_SYN_RECEIVED || 22559077f387SGleb Smirnoff tp->t_state == TCPS_SYN_SENT) 22569077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 22579077f387SGleb Smirnoff TP_KEEPINIT(tp)); 22589077f387SGleb Smirnoff break; 22599077f387SGleb Smirnoff } 226009fe6320SNavdeep Parhar goto unlock_and_done; 22619077f387SGleb Smirnoff 226285c05144SGleb Smirnoff case TCP_KEEPCNT: 226385c05144SGleb Smirnoff INP_WUNLOCK(inp); 226485c05144SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 226585c05144SGleb Smirnoff if (error) 226685c05144SGleb Smirnoff return (error); 226785c05144SGleb Smirnoff 226885c05144SGleb Smirnoff INP_WLOCK_RECHECK(inp); 226985c05144SGleb Smirnoff tp->t_keepcnt = ui; 227085c05144SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 227185c05144SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 227285c05144SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 227385c05144SGleb Smirnoff TP_MAXIDLE(tp)); 227485c05144SGleb Smirnoff goto unlock_and_done; 227585c05144SGleb Smirnoff 227686a996e6SHiren Panchasara #ifdef TCPPCAP 227786a996e6SHiren Panchasara case TCP_PCAP_OUT: 227886a996e6SHiren Panchasara case TCP_PCAP_IN: 227986a996e6SHiren Panchasara INP_WUNLOCK(inp); 228086a996e6SHiren Panchasara error = sooptcopyin(sopt, &optval, sizeof optval, 228186a996e6SHiren Panchasara sizeof optval); 228286a996e6SHiren Panchasara if (error) 228386a996e6SHiren Panchasara return (error); 228486a996e6SHiren Panchasara 228586a996e6SHiren Panchasara INP_WLOCK_RECHECK(inp); 228686a996e6SHiren Panchasara if (optval >= 0) 228786a996e6SHiren Panchasara tcp_pcap_set_sock_max(TCP_PCAP_OUT ? 228886a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts), 228986a996e6SHiren Panchasara optval); 229086a996e6SHiren Panchasara else 229186a996e6SHiren Panchasara error = EINVAL; 229286a996e6SHiren Panchasara goto unlock_and_done; 229386a996e6SHiren Panchasara #endif 229486a996e6SHiren Panchasara 2295c560df6fSPatrick Kelsey case TCP_FASTOPEN: { 2296c560df6fSPatrick Kelsey struct tcp_fastopen tfo_optval; 2297c560df6fSPatrick Kelsey 2298281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2299c560df6fSPatrick Kelsey if (!V_tcp_fastopen_client_enable && 2300c560df6fSPatrick Kelsey !V_tcp_fastopen_server_enable) 2301281a0fd4SPatrick Kelsey return (EPERM); 2302281a0fd4SPatrick Kelsey 2303c560df6fSPatrick Kelsey error = sooptcopyin(sopt, &tfo_optval, 2304c560df6fSPatrick Kelsey sizeof(tfo_optval), sizeof(int)); 2305281a0fd4SPatrick Kelsey if (error) 2306281a0fd4SPatrick Kelsey return (error); 2307281a0fd4SPatrick Kelsey 2308281a0fd4SPatrick Kelsey INP_WLOCK_RECHECK(inp); 2309d442a657SMichael Tuexen if ((tp->t_state != TCPS_CLOSED) && 2310d442a657SMichael Tuexen (tp->t_state != TCPS_LISTEN)) { 2311d442a657SMichael Tuexen error = EINVAL; 2312d442a657SMichael Tuexen goto unlock_and_done; 2313d442a657SMichael Tuexen } 2314c560df6fSPatrick Kelsey if (tfo_optval.enable) { 2315c560df6fSPatrick Kelsey if (tp->t_state == TCPS_LISTEN) { 2316c560df6fSPatrick Kelsey if (!V_tcp_fastopen_server_enable) { 2317c560df6fSPatrick Kelsey error = EPERM; 2318c560df6fSPatrick Kelsey goto unlock_and_done; 2319c560df6fSPatrick Kelsey } 2320c560df6fSPatrick Kelsey 2321c560df6fSPatrick Kelsey if (tp->t_tfo_pending == NULL) 2322281a0fd4SPatrick Kelsey tp->t_tfo_pending = 2323281a0fd4SPatrick Kelsey tcp_fastopen_alloc_counter(); 2324c560df6fSPatrick Kelsey } else { 2325c560df6fSPatrick Kelsey /* 2326c560df6fSPatrick Kelsey * If a pre-shared key was provided, 2327c560df6fSPatrick Kelsey * stash it in the client cookie 2328c560df6fSPatrick Kelsey * field of the tcpcb for use during 2329c560df6fSPatrick Kelsey * connect. 2330c560df6fSPatrick Kelsey */ 2331c560df6fSPatrick Kelsey if (sopt->sopt_valsize == 2332c560df6fSPatrick Kelsey sizeof(tfo_optval)) { 2333c560df6fSPatrick Kelsey memcpy(tp->t_tfo_cookie.client, 2334c560df6fSPatrick Kelsey tfo_optval.psk, 2335c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN); 2336c560df6fSPatrick Kelsey tp->t_tfo_client_cookie_len = 2337c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN; 2338c560df6fSPatrick Kelsey } 2339c560df6fSPatrick Kelsey } 2340d442a657SMichael Tuexen tp->t_flags |= TF_FASTOPEN; 2341281a0fd4SPatrick Kelsey } else 2342281a0fd4SPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 2343281a0fd4SPatrick Kelsey goto unlock_and_done; 2344c560df6fSPatrick Kelsey } 2345281a0fd4SPatrick Kelsey 2346e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 23472529f56eSJonathan T. Looney case TCP_LOG: 23482529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23492529f56eSJonathan T. Looney error = sooptcopyin(sopt, &optval, sizeof optval, 23502529f56eSJonathan T. Looney sizeof optval); 23512529f56eSJonathan T. Looney if (error) 23522529f56eSJonathan T. Looney return (error); 23532529f56eSJonathan T. Looney 23542529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 23552529f56eSJonathan T. Looney error = tcp_log_state_change(tp, optval); 23562529f56eSJonathan T. Looney goto unlock_and_done; 23572529f56eSJonathan T. Looney 23582529f56eSJonathan T. Looney case TCP_LOGBUF: 23592529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23602529f56eSJonathan T. Looney error = EINVAL; 23612529f56eSJonathan T. Looney break; 23622529f56eSJonathan T. Looney 23632529f56eSJonathan T. Looney case TCP_LOGID: 23642529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23652529f56eSJonathan T. Looney error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0); 23662529f56eSJonathan T. Looney if (error) 23672529f56eSJonathan T. Looney break; 23682529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 23692529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 23702529f56eSJonathan T. Looney error = tcp_log_set_id(tp, buf); 23712529f56eSJonathan T. Looney /* tcp_log_set_id() unlocks the INP. */ 23722529f56eSJonathan T. Looney break; 23732529f56eSJonathan T. Looney 23742529f56eSJonathan T. Looney case TCP_LOGDUMP: 23752529f56eSJonathan T. Looney case TCP_LOGDUMPID: 23762529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23772529f56eSJonathan T. Looney error = 23782529f56eSJonathan T. Looney sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0); 23792529f56eSJonathan T. Looney if (error) 23802529f56eSJonathan T. Looney break; 23812529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 23822529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 23832529f56eSJonathan T. Looney if (sopt->sopt_name == TCP_LOGDUMP) { 23842529f56eSJonathan T. Looney error = tcp_log_dump_tp_logbuf(tp, buf, 23852529f56eSJonathan T. Looney M_WAITOK, true); 23862529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23872529f56eSJonathan T. Looney } else { 23882529f56eSJonathan T. Looney tcp_log_dump_tp_bucket_logbufs(tp, buf); 23892529f56eSJonathan T. Looney /* 23902529f56eSJonathan T. Looney * tcp_log_dump_tp_bucket_logbufs() drops the 23912529f56eSJonathan T. Looney * INP lock. 23922529f56eSJonathan T. Looney */ 23932529f56eSJonathan T. Looney } 23942529f56eSJonathan T. Looney break; 2395e24e5683SJonathan T. Looney #endif 23962529f56eSJonathan T. Looney 2397df8bae1dSRodney W. Grimes default: 23988501a69cSRobert Watson INP_WUNLOCK(inp); 2399df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2400df8bae1dSRodney W. Grimes break; 2401df8bae1dSRodney W. Grimes } 2402df8bae1dSRodney W. Grimes break; 2403df8bae1dSRodney W. Grimes 2404cfe8b629SGarrett Wollman case SOPT_GET: 24051e8f5ffaSRobert Watson tp = intotcpcb(inp); 2406cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2407fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 240888f6b043SBruce M Simpson case TCP_MD5SIG: 24098501a69cSRobert Watson INP_WUNLOCK(inp); 241097453e5eSClaudio Jeker if (!TCPMD5_ENABLED()) 2411fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 2412fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 24131cfd4b53SBruce M Simpson break; 2414265ed012SBruce M Simpson #endif 24151e8f5ffaSRobert Watson 2416df8bae1dSRodney W. Grimes case TCP_NODELAY: 2417cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 24188501a69cSRobert Watson INP_WUNLOCK(inp); 2419b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2420df8bae1dSRodney W. Grimes break; 2421df8bae1dSRodney W. Grimes case TCP_MAXSEG: 2422cfe8b629SGarrett Wollman optval = tp->t_maxseg; 24238501a69cSRobert Watson INP_WUNLOCK(inp); 2424b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2425df8bae1dSRodney W. Grimes break; 24269e644c23SMichael Tuexen case TCP_REMOTE_UDP_ENCAPS_PORT: 24279e644c23SMichael Tuexen optval = ntohs(tp->t_port); 24289e644c23SMichael Tuexen INP_WUNLOCK(inp); 24299e644c23SMichael Tuexen error = sooptcopyout(sopt, &optval, sizeof optval); 24309e644c23SMichael Tuexen break; 2431a0292f23SGarrett Wollman case TCP_NOOPT: 2432cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 24338501a69cSRobert Watson INP_WUNLOCK(inp); 2434b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2435a0292f23SGarrett Wollman break; 2436a0292f23SGarrett Wollman case TCP_NOPUSH: 2437cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 24388501a69cSRobert Watson INP_WUNLOCK(inp); 2439b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2440b8af5dfaSRobert Watson break; 2441b8af5dfaSRobert Watson case TCP_INFO: 2442b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 24438501a69cSRobert Watson INP_WUNLOCK(inp); 2444b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 2445a0292f23SGarrett Wollman break; 2446adc56f5aSEdward Tomasz Napierala case TCP_STATS: 2447adc56f5aSEdward Tomasz Napierala { 2448adc56f5aSEdward Tomasz Napierala #ifdef STATS 2449adc56f5aSEdward Tomasz Napierala int nheld; 2450adc56f5aSEdward Tomasz Napierala TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0; 2451adc56f5aSEdward Tomasz Napierala 2452adc56f5aSEdward Tomasz Napierala error = 0; 2453adc56f5aSEdward Tomasz Napierala socklen_t outsbsz = sopt->sopt_valsize; 2454adc56f5aSEdward Tomasz Napierala if (tp->t_stats == NULL) 2455adc56f5aSEdward Tomasz Napierala error = ENOENT; 2456adc56f5aSEdward Tomasz Napierala else if (outsbsz >= tp->t_stats->cursz) 2457adc56f5aSEdward Tomasz Napierala outsbsz = tp->t_stats->cursz; 2458adc56f5aSEdward Tomasz Napierala else if (outsbsz >= sizeof(struct statsblob)) 2459adc56f5aSEdward Tomasz Napierala outsbsz = sizeof(struct statsblob); 2460adc56f5aSEdward Tomasz Napierala else 2461adc56f5aSEdward Tomasz Napierala error = EINVAL; 2462adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2463adc56f5aSEdward Tomasz Napierala if (error) 2464adc56f5aSEdward Tomasz Napierala break; 2465adc56f5aSEdward Tomasz Napierala 2466adc56f5aSEdward Tomasz Napierala sbp = sopt->sopt_val; 2467adc56f5aSEdward Tomasz Napierala nheld = atop(round_page(((vm_offset_t)sbp) + 2468adc56f5aSEdward Tomasz Napierala (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp)); 2469adc56f5aSEdward Tomasz Napierala vm_page_t ma[nheld]; 2470adc56f5aSEdward Tomasz Napierala if (vm_fault_quick_hold_pages( 2471adc56f5aSEdward Tomasz Napierala &curproc->p_vmspace->vm_map, (vm_offset_t)sbp, 2472adc56f5aSEdward Tomasz Napierala outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma, 2473adc56f5aSEdward Tomasz Napierala nheld) < 0) { 2474adc56f5aSEdward Tomasz Napierala error = EFAULT; 2475adc56f5aSEdward Tomasz Napierala break; 2476adc56f5aSEdward Tomasz Napierala } 2477adc56f5aSEdward Tomasz Napierala 2478adc56f5aSEdward Tomasz Napierala if ((error = copyin_nofault(&(sbp->flags), &sbflags, 2479adc56f5aSEdward Tomasz Napierala SIZEOF_MEMBER(struct statsblob, flags)))) 2480adc56f5aSEdward Tomasz Napierala goto unhold; 2481adc56f5aSEdward Tomasz Napierala 2482adc56f5aSEdward Tomasz Napierala INP_WLOCK_RECHECK(inp); 2483adc56f5aSEdward Tomasz Napierala error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats, 2484adc56f5aSEdward Tomasz Napierala sbflags | SB_CLONE_USRDSTNOFAULT); 2485adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2486adc56f5aSEdward Tomasz Napierala sopt->sopt_valsize = outsbsz; 2487adc56f5aSEdward Tomasz Napierala unhold: 2488adc56f5aSEdward Tomasz Napierala vm_page_unhold_pages(ma, nheld); 2489adc56f5aSEdward Tomasz Napierala #else 2490adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2491adc56f5aSEdward Tomasz Napierala error = EOPNOTSUPP; 2492adc56f5aSEdward Tomasz Napierala #endif /* !STATS */ 2493adc56f5aSEdward Tomasz Napierala break; 2494adc56f5aSEdward Tomasz Napierala } 2495dbc42409SLawrence Stewart case TCP_CONGESTION: 2496af6fef3aSGleb Smirnoff len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 2497dbc42409SLawrence Stewart INP_WUNLOCK(inp); 2498af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, buf, len + 1); 2499dbc42409SLawrence Stewart break; 250008af8aacSRandall Stewart case TCP_MAXUNACKTIME: 25012f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 25022f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 25032f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 25042f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 25052f3eb7f4SGleb Smirnoff switch (sopt->sopt_name) { 250608af8aacSRandall Stewart case TCP_MAXUNACKTIME: 250708af8aacSRandall Stewart ui = TP_MAXUNACKTIME(tp) / hz; 250808af8aacSRandall Stewart break; 25092f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 25105a17b6adSMichael Tuexen ui = TP_KEEPIDLE(tp) / hz; 25112f3eb7f4SGleb Smirnoff break; 25122f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 25135a17b6adSMichael Tuexen ui = TP_KEEPINTVL(tp) / hz; 25142f3eb7f4SGleb Smirnoff break; 25152f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 25165a17b6adSMichael Tuexen ui = TP_KEEPINIT(tp) / hz; 25172f3eb7f4SGleb Smirnoff break; 25182f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 25195a17b6adSMichael Tuexen ui = TP_KEEPCNT(tp); 25202f3eb7f4SGleb Smirnoff break; 25212f3eb7f4SGleb Smirnoff } 25222f3eb7f4SGleb Smirnoff INP_WUNLOCK(inp); 25232f3eb7f4SGleb Smirnoff error = sooptcopyout(sopt, &ui, sizeof(ui)); 25242f3eb7f4SGleb Smirnoff break; 252586a996e6SHiren Panchasara #ifdef TCPPCAP 252686a996e6SHiren Panchasara case TCP_PCAP_OUT: 252786a996e6SHiren Panchasara case TCP_PCAP_IN: 252886a996e6SHiren Panchasara optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ? 252986a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts)); 253086a996e6SHiren Panchasara INP_WUNLOCK(inp); 253186a996e6SHiren Panchasara error = sooptcopyout(sopt, &optval, sizeof optval); 253286a996e6SHiren Panchasara break; 253386a996e6SHiren Panchasara #endif 2534281a0fd4SPatrick Kelsey case TCP_FASTOPEN: 2535281a0fd4SPatrick Kelsey optval = tp->t_flags & TF_FASTOPEN; 2536281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2537281a0fd4SPatrick Kelsey error = sooptcopyout(sopt, &optval, sizeof optval); 2538281a0fd4SPatrick Kelsey break; 2539e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 25402529f56eSJonathan T. Looney case TCP_LOG: 25412529f56eSJonathan T. Looney optval = tp->t_logstate; 25422529f56eSJonathan T. Looney INP_WUNLOCK(inp); 25432529f56eSJonathan T. Looney error = sooptcopyout(sopt, &optval, sizeof(optval)); 25442529f56eSJonathan T. Looney break; 25452529f56eSJonathan T. Looney case TCP_LOGBUF: 25462529f56eSJonathan T. Looney /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */ 25472529f56eSJonathan T. Looney error = tcp_log_getlogbuf(sopt, tp); 25482529f56eSJonathan T. Looney break; 25492529f56eSJonathan T. Looney case TCP_LOGID: 25502529f56eSJonathan T. Looney len = tcp_log_get_id(tp, buf); 25512529f56eSJonathan T. Looney INP_WUNLOCK(inp); 25522529f56eSJonathan T. Looney error = sooptcopyout(sopt, buf, len + 1); 25532529f56eSJonathan T. Looney break; 25542529f56eSJonathan T. Looney case TCP_LOGDUMP: 25552529f56eSJonathan T. Looney case TCP_LOGDUMPID: 25562529f56eSJonathan T. Looney INP_WUNLOCK(inp); 25572529f56eSJonathan T. Looney error = EINVAL; 25582529f56eSJonathan T. Looney break; 2559e24e5683SJonathan T. Looney #endif 2560b2e60773SJohn Baldwin #ifdef KERN_TLS 2561b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2562fd7daa72SMichael Tuexen error = ktls_get_tx_mode(so, &optval); 2563b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2564bf256782SMark Johnston if (error == 0) 2565bf256782SMark Johnston error = sooptcopyout(sopt, &optval, 2566bf256782SMark Johnston sizeof(optval)); 2567b2e60773SJohn Baldwin break; 2568f1f93475SJohn Baldwin case TCP_RXTLS_MODE: 2569fd7daa72SMichael Tuexen error = ktls_get_rx_mode(so, &optval); 2570f1f93475SJohn Baldwin INP_WUNLOCK(inp); 2571bf256782SMark Johnston if (error == 0) 2572bf256782SMark Johnston error = sooptcopyout(sopt, &optval, 2573bf256782SMark Johnston sizeof(optval)); 2574f1f93475SJohn Baldwin break; 2575b2e60773SJohn Baldwin #endif 25760471a8c7SRichard Scheffenegger case TCP_LRD: 25770471a8c7SRichard Scheffenegger optval = tp->t_flags & TF_LRD; 25780471a8c7SRichard Scheffenegger INP_WUNLOCK(inp); 25790471a8c7SRichard Scheffenegger error = sooptcopyout(sopt, &optval, sizeof optval); 25800471a8c7SRichard Scheffenegger break; 2581df8bae1dSRodney W. Grimes default: 25828501a69cSRobert Watson INP_WUNLOCK(inp); 2583df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2584df8bae1dSRodney W. Grimes break; 2585df8bae1dSRodney W. Grimes } 2586df8bae1dSRodney W. Grimes break; 2587df8bae1dSRodney W. Grimes } 2588df8bae1dSRodney W. Grimes return (error); 2589df8bae1dSRodney W. Grimes } 25908501a69cSRobert Watson #undef INP_WLOCK_RECHECK 2591bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP 2592df8bae1dSRodney W. Grimes 259326e30fbbSDavid Greenman /* 2594df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 2595df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 2596df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 2597df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 2598df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 2599df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 2600df8bae1dSRodney W. Grimes */ 2601623dce13SRobert Watson static void 2602ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp) 2603df8bae1dSRodney W. Grimes { 26049eb0e832SGleb Smirnoff struct inpcb *inp = tptoinpcb(tp); 26059eb0e832SGleb Smirnoff struct socket *so = tptosocket(tp); 2606e6e0b5ffSRobert Watson 260797a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 26088501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2609df8bae1dSRodney W. Grimes 2610623dce13SRobert Watson /* 2611623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 2612623dce13SRobert Watson * socket is still open. 2613623dce13SRobert Watson */ 26148db239dcSMichael Tuexen if (tp->t_state < TCPS_ESTABLISHED && 26158db239dcSMichael Tuexen !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) { 2616df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2617623dce13SRobert Watson KASSERT(tp != NULL, 2618623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 2619623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 2620243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 2621623dce13SRobert Watson KASSERT(tp != NULL, 2622623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 2623623dce13SRobert Watson } else { 2624df8bae1dSRodney W. Grimes soisdisconnecting(so); 2625df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 2626623dce13SRobert Watson tcp_usrclosed(tp); 2627ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 2628f64dc2abSGleb Smirnoff /* Ignore stack's drop request, we already at it. */ 2629f64dc2abSGleb Smirnoff (void)tcp_output_nodrop(tp); 2630df8bae1dSRodney W. Grimes } 2631df8bae1dSRodney W. Grimes } 2632df8bae1dSRodney W. Grimes 2633df8bae1dSRodney W. Grimes /* 2634df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 2635df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 2636df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 2637df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 2638df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 2639df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 2640df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 2641df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 2642df8bae1dSRodney W. Grimes */ 2643623dce13SRobert Watson static void 2644ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp) 2645df8bae1dSRodney W. Grimes { 2646df8bae1dSRodney W. Grimes 264797a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 26489eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 2649e6e0b5ffSRobert Watson 2650df8bae1dSRodney W. Grimes switch (tp->t_state) { 2651df8bae1dSRodney W. Grimes case TCPS_LISTEN: 265209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 265309fe6320SNavdeep Parhar tcp_offload_listen_stop(tp); 265409fe6320SNavdeep Parhar #endif 2655550e9d42SHiren Panchasara tcp_state_change(tp, TCPS_CLOSED); 2656bc65987aSKip Macy /* FALLTHROUGH */ 2657bc65987aSKip Macy case TCPS_CLOSED: 2658df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2659623dce13SRobert Watson /* 2660623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 2661623dce13SRobert Watson * still open. 2662623dce13SRobert Watson */ 2663623dce13SRobert Watson KASSERT(tp != NULL, 2664623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 2665df8bae1dSRodney W. Grimes break; 2666df8bae1dSRodney W. Grimes 2667a0292f23SGarrett Wollman case TCPS_SYN_SENT: 2668df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2669a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 2670a0292f23SGarrett Wollman break; 2671a0292f23SGarrett Wollman 2672df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 267357f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2674df8bae1dSRodney W. Grimes break; 2675df8bae1dSRodney W. Grimes 2676df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 267757f60867SMark Johnston tcp_state_change(tp, TCPS_LAST_ACK); 2678df8bae1dSRodney W. Grimes break; 2679df8bae1dSRodney W. Grimes } 268008af8aacSRandall Stewart if (tp->t_acktime == 0) 268108af8aacSRandall Stewart tp->t_acktime = ticks; 2682abc7d910SRobert Watson if (tp->t_state >= TCPS_FIN_WAIT_2) { 26839eb0e832SGleb Smirnoff soisdisconnected(tptosocket(tp)); 2684abc7d910SRobert Watson /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 26857c72af87SMohan Srinivasan if (tp->t_state == TCPS_FIN_WAIT_2) { 26867c72af87SMohan Srinivasan int timeout; 26877c72af87SMohan Srinivasan 26887c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 26899077f387SGleb Smirnoff tcp_finwait2_timeout : TP_MAXIDLE(tp); 2690b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 2691b6239c4aSAndras Olah } 2692df8bae1dSRodney W. Grimes } 26937c72af87SMohan Srinivasan } 2694497057eeSRobert Watson 2695497057eeSRobert Watson #ifdef DDB 2696497057eeSRobert Watson static void 2697497057eeSRobert Watson db_print_indent(int indent) 2698497057eeSRobert Watson { 2699497057eeSRobert Watson int i; 2700497057eeSRobert Watson 2701497057eeSRobert Watson for (i = 0; i < indent; i++) 2702497057eeSRobert Watson db_printf(" "); 2703497057eeSRobert Watson } 2704497057eeSRobert Watson 2705497057eeSRobert Watson static void 2706497057eeSRobert Watson db_print_tstate(int t_state) 2707497057eeSRobert Watson { 2708497057eeSRobert Watson 2709497057eeSRobert Watson switch (t_state) { 2710497057eeSRobert Watson case TCPS_CLOSED: 2711497057eeSRobert Watson db_printf("TCPS_CLOSED"); 2712497057eeSRobert Watson return; 2713497057eeSRobert Watson 2714497057eeSRobert Watson case TCPS_LISTEN: 2715497057eeSRobert Watson db_printf("TCPS_LISTEN"); 2716497057eeSRobert Watson return; 2717497057eeSRobert Watson 2718497057eeSRobert Watson case TCPS_SYN_SENT: 2719497057eeSRobert Watson db_printf("TCPS_SYN_SENT"); 2720497057eeSRobert Watson return; 2721497057eeSRobert Watson 2722497057eeSRobert Watson case TCPS_SYN_RECEIVED: 2723497057eeSRobert Watson db_printf("TCPS_SYN_RECEIVED"); 2724497057eeSRobert Watson return; 2725497057eeSRobert Watson 2726497057eeSRobert Watson case TCPS_ESTABLISHED: 2727497057eeSRobert Watson db_printf("TCPS_ESTABLISHED"); 2728497057eeSRobert Watson return; 2729497057eeSRobert Watson 2730497057eeSRobert Watson case TCPS_CLOSE_WAIT: 2731497057eeSRobert Watson db_printf("TCPS_CLOSE_WAIT"); 2732497057eeSRobert Watson return; 2733497057eeSRobert Watson 2734497057eeSRobert Watson case TCPS_FIN_WAIT_1: 2735497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_1"); 2736497057eeSRobert Watson return; 2737497057eeSRobert Watson 2738497057eeSRobert Watson case TCPS_CLOSING: 2739497057eeSRobert Watson db_printf("TCPS_CLOSING"); 2740497057eeSRobert Watson return; 2741497057eeSRobert Watson 2742497057eeSRobert Watson case TCPS_LAST_ACK: 2743497057eeSRobert Watson db_printf("TCPS_LAST_ACK"); 2744497057eeSRobert Watson return; 2745497057eeSRobert Watson 2746497057eeSRobert Watson case TCPS_FIN_WAIT_2: 2747497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_2"); 2748497057eeSRobert Watson return; 2749497057eeSRobert Watson 2750497057eeSRobert Watson case TCPS_TIME_WAIT: 2751497057eeSRobert Watson db_printf("TCPS_TIME_WAIT"); 2752497057eeSRobert Watson return; 2753497057eeSRobert Watson 2754497057eeSRobert Watson default: 2755497057eeSRobert Watson db_printf("unknown"); 2756497057eeSRobert Watson return; 2757497057eeSRobert Watson } 2758497057eeSRobert Watson } 2759497057eeSRobert Watson 2760497057eeSRobert Watson static void 2761497057eeSRobert Watson db_print_tflags(u_int t_flags) 2762497057eeSRobert Watson { 2763497057eeSRobert Watson int comma; 2764497057eeSRobert Watson 2765497057eeSRobert Watson comma = 0; 2766497057eeSRobert Watson if (t_flags & TF_ACKNOW) { 2767497057eeSRobert Watson db_printf("%sTF_ACKNOW", comma ? ", " : ""); 2768497057eeSRobert Watson comma = 1; 2769497057eeSRobert Watson } 2770497057eeSRobert Watson if (t_flags & TF_DELACK) { 2771497057eeSRobert Watson db_printf("%sTF_DELACK", comma ? ", " : ""); 2772497057eeSRobert Watson comma = 1; 2773497057eeSRobert Watson } 2774497057eeSRobert Watson if (t_flags & TF_NODELAY) { 2775497057eeSRobert Watson db_printf("%sTF_NODELAY", comma ? ", " : ""); 2776497057eeSRobert Watson comma = 1; 2777497057eeSRobert Watson } 2778497057eeSRobert Watson if (t_flags & TF_NOOPT) { 2779497057eeSRobert Watson db_printf("%sTF_NOOPT", comma ? ", " : ""); 2780497057eeSRobert Watson comma = 1; 2781497057eeSRobert Watson } 2782497057eeSRobert Watson if (t_flags & TF_SENTFIN) { 2783497057eeSRobert Watson db_printf("%sTF_SENTFIN", comma ? ", " : ""); 2784497057eeSRobert Watson comma = 1; 2785497057eeSRobert Watson } 2786497057eeSRobert Watson if (t_flags & TF_REQ_SCALE) { 2787497057eeSRobert Watson db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 2788497057eeSRobert Watson comma = 1; 2789497057eeSRobert Watson } 2790497057eeSRobert Watson if (t_flags & TF_RCVD_SCALE) { 2791497057eeSRobert Watson db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 2792497057eeSRobert Watson comma = 1; 2793497057eeSRobert Watson } 2794497057eeSRobert Watson if (t_flags & TF_REQ_TSTMP) { 2795497057eeSRobert Watson db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 2796497057eeSRobert Watson comma = 1; 2797497057eeSRobert Watson } 2798497057eeSRobert Watson if (t_flags & TF_RCVD_TSTMP) { 2799497057eeSRobert Watson db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 2800497057eeSRobert Watson comma = 1; 2801497057eeSRobert Watson } 2802497057eeSRobert Watson if (t_flags & TF_SACK_PERMIT) { 2803497057eeSRobert Watson db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 2804497057eeSRobert Watson comma = 1; 2805497057eeSRobert Watson } 2806497057eeSRobert Watson if (t_flags & TF_NEEDSYN) { 2807497057eeSRobert Watson db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 2808497057eeSRobert Watson comma = 1; 2809497057eeSRobert Watson } 2810497057eeSRobert Watson if (t_flags & TF_NEEDFIN) { 2811497057eeSRobert Watson db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 2812497057eeSRobert Watson comma = 1; 2813497057eeSRobert Watson } 2814497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 2815497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 2816497057eeSRobert Watson comma = 1; 2817497057eeSRobert Watson } 28183f169c54SRichard Scheffenegger if (t_flags & TF_PREVVALID) { 28193f169c54SRichard Scheffenegger db_printf("%sTF_PREVVALID", comma ? ", " : ""); 28203f169c54SRichard Scheffenegger comma = 1; 28213f169c54SRichard Scheffenegger } 2822497057eeSRobert Watson if (t_flags & TF_MORETOCOME) { 2823497057eeSRobert Watson db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 2824497057eeSRobert Watson comma = 1; 2825497057eeSRobert Watson } 2826493105c2SGleb Smirnoff if (t_flags & TF_SONOTCONN) { 2827493105c2SGleb Smirnoff db_printf("%sTF_SONOTCONN", comma ? ", " : ""); 2828497057eeSRobert Watson comma = 1; 2829497057eeSRobert Watson } 2830497057eeSRobert Watson if (t_flags & TF_LASTIDLE) { 2831497057eeSRobert Watson db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 2832497057eeSRobert Watson comma = 1; 2833497057eeSRobert Watson } 2834497057eeSRobert Watson if (t_flags & TF_RXWIN0SENT) { 2835497057eeSRobert Watson db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 2836497057eeSRobert Watson comma = 1; 2837497057eeSRobert Watson } 2838497057eeSRobert Watson if (t_flags & TF_FASTRECOVERY) { 2839497057eeSRobert Watson db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 2840497057eeSRobert Watson comma = 1; 2841497057eeSRobert Watson } 2842dbc42409SLawrence Stewart if (t_flags & TF_CONGRECOVERY) { 2843dbc42409SLawrence Stewart db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 2844dbc42409SLawrence Stewart comma = 1; 2845dbc42409SLawrence Stewart } 2846497057eeSRobert Watson if (t_flags & TF_WASFRECOVERY) { 2847497057eeSRobert Watson db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 2848497057eeSRobert Watson comma = 1; 2849497057eeSRobert Watson } 28503f169c54SRichard Scheffenegger if (t_flags & TF_WASCRECOVERY) { 28513f169c54SRichard Scheffenegger db_printf("%sTF_WASCRECOVERY", comma ? ", " : ""); 28523f169c54SRichard Scheffenegger comma = 1; 28533f169c54SRichard Scheffenegger } 2854497057eeSRobert Watson if (t_flags & TF_SIGNATURE) { 2855497057eeSRobert Watson db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 2856497057eeSRobert Watson comma = 1; 2857497057eeSRobert Watson } 2858497057eeSRobert Watson if (t_flags & TF_FORCEDATA) { 2859497057eeSRobert Watson db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 2860497057eeSRobert Watson comma = 1; 2861497057eeSRobert Watson } 2862497057eeSRobert Watson if (t_flags & TF_TSO) { 2863497057eeSRobert Watson db_printf("%sTF_TSO", comma ? ", " : ""); 2864497057eeSRobert Watson comma = 1; 2865497057eeSRobert Watson } 2866281a0fd4SPatrick Kelsey if (t_flags & TF_FASTOPEN) { 2867281a0fd4SPatrick Kelsey db_printf("%sTF_FASTOPEN", comma ? ", " : ""); 2868281a0fd4SPatrick Kelsey comma = 1; 2869281a0fd4SPatrick Kelsey } 2870497057eeSRobert Watson } 2871497057eeSRobert Watson 2872497057eeSRobert Watson static void 28733cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2) 28743cf38784SMichael Tuexen { 28753cf38784SMichael Tuexen int comma; 28763cf38784SMichael Tuexen 28773cf38784SMichael Tuexen comma = 0; 28783f169c54SRichard Scheffenegger if (t_flags2 & TF2_PLPMTU_BLACKHOLE) { 28793f169c54SRichard Scheffenegger db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : ""); 28803f169c54SRichard Scheffenegger comma = 1; 28813f169c54SRichard Scheffenegger } 28823f169c54SRichard Scheffenegger if (t_flags2 & TF2_PLPMTU_PMTUD) { 28833f169c54SRichard Scheffenegger db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : ""); 28843f169c54SRichard Scheffenegger comma = 1; 28853f169c54SRichard Scheffenegger } 28863f169c54SRichard Scheffenegger if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) { 28873f169c54SRichard Scheffenegger db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : ""); 28883f169c54SRichard Scheffenegger comma = 1; 28893f169c54SRichard Scheffenegger } 28903f169c54SRichard Scheffenegger if (t_flags2 & TF2_LOG_AUTO) { 28913f169c54SRichard Scheffenegger db_printf("%sTF2_LOG_AUTO", comma ? ", " : ""); 28923f169c54SRichard Scheffenegger comma = 1; 28933f169c54SRichard Scheffenegger } 28943f169c54SRichard Scheffenegger if (t_flags2 & TF2_DROP_AF_DATA) { 28953f169c54SRichard Scheffenegger db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : ""); 28963f169c54SRichard Scheffenegger comma = 1; 28973f169c54SRichard Scheffenegger } 28983cf38784SMichael Tuexen if (t_flags2 & TF2_ECN_PERMIT) { 28993cf38784SMichael Tuexen db_printf("%sTF2_ECN_PERMIT", comma ? ", " : ""); 29003cf38784SMichael Tuexen comma = 1; 29013cf38784SMichael Tuexen } 29023f169c54SRichard Scheffenegger if (t_flags2 & TF2_ECN_SND_CWR) { 29033f169c54SRichard Scheffenegger db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : ""); 29043f169c54SRichard Scheffenegger comma = 1; 29053f169c54SRichard Scheffenegger } 29063f169c54SRichard Scheffenegger if (t_flags2 & TF2_ECN_SND_ECE) { 29073f169c54SRichard Scheffenegger db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : ""); 29083f169c54SRichard Scheffenegger comma = 1; 29093f169c54SRichard Scheffenegger } 29103f169c54SRichard Scheffenegger if (t_flags2 & TF2_ACE_PERMIT) { 29113f169c54SRichard Scheffenegger db_printf("%sTF2_ACE_PERMIT", comma ? ", " : ""); 29123f169c54SRichard Scheffenegger comma = 1; 29133f169c54SRichard Scheffenegger } 29143f169c54SRichard Scheffenegger if (t_flags2 & TF2_FBYTES_COMPLETE) { 29153f169c54SRichard Scheffenegger db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : ""); 29163f169c54SRichard Scheffenegger comma = 1; 29173f169c54SRichard Scheffenegger } 29183cf38784SMichael Tuexen } 29193cf38784SMichael Tuexen 29203cf38784SMichael Tuexen static void 2921497057eeSRobert Watson db_print_toobflags(char t_oobflags) 2922497057eeSRobert Watson { 2923497057eeSRobert Watson int comma; 2924497057eeSRobert Watson 2925497057eeSRobert Watson comma = 0; 2926497057eeSRobert Watson if (t_oobflags & TCPOOB_HAVEDATA) { 2927497057eeSRobert Watson db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 2928497057eeSRobert Watson comma = 1; 2929497057eeSRobert Watson } 2930497057eeSRobert Watson if (t_oobflags & TCPOOB_HADDATA) { 2931497057eeSRobert Watson db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 2932497057eeSRobert Watson comma = 1; 2933497057eeSRobert Watson } 2934497057eeSRobert Watson } 2935497057eeSRobert Watson 2936497057eeSRobert Watson static void 2937497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 2938497057eeSRobert Watson { 2939497057eeSRobert Watson 2940497057eeSRobert Watson db_print_indent(indent); 2941497057eeSRobert Watson db_printf("%s at %p\n", name, tp); 2942497057eeSRobert Watson 2943497057eeSRobert Watson indent += 2; 2944497057eeSRobert Watson 2945497057eeSRobert Watson db_print_indent(indent); 2946497057eeSRobert Watson db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 2947c28440dbSRandall Stewart TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 2948497057eeSRobert Watson 2949497057eeSRobert Watson db_print_indent(indent); 2950446ccdd0SGleb Smirnoff db_printf("t_callout: %p t_timers: %p\n", 2951446ccdd0SGleb Smirnoff &tp->t_callout, &tp->t_timers); 2952497057eeSRobert Watson 2953497057eeSRobert Watson db_print_indent(indent); 2954497057eeSRobert Watson db_printf("t_state: %d (", tp->t_state); 2955497057eeSRobert Watson db_print_tstate(tp->t_state); 2956497057eeSRobert Watson db_printf(")\n"); 2957497057eeSRobert Watson 2958497057eeSRobert Watson db_print_indent(indent); 2959497057eeSRobert Watson db_printf("t_flags: 0x%x (", tp->t_flags); 2960497057eeSRobert Watson db_print_tflags(tp->t_flags); 2961497057eeSRobert Watson db_printf(")\n"); 2962497057eeSRobert Watson 2963497057eeSRobert Watson db_print_indent(indent); 29643cf38784SMichael Tuexen db_printf("t_flags2: 0x%x (", tp->t_flags2); 29653cf38784SMichael Tuexen db_print_tflags2(tp->t_flags2); 29663cf38784SMichael Tuexen db_printf(")\n"); 29673cf38784SMichael Tuexen 29683cf38784SMichael Tuexen db_print_indent(indent); 2969fb8f221aSMaxim Konovalov db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: 0x%08x\n", 2970497057eeSRobert Watson tp->snd_una, tp->snd_max, tp->snd_nxt); 2971497057eeSRobert Watson 2972497057eeSRobert Watson db_print_indent(indent); 2973497057eeSRobert Watson db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 2974497057eeSRobert Watson tp->snd_up, tp->snd_wl1, tp->snd_wl2); 2975497057eeSRobert Watson 2976497057eeSRobert Watson db_print_indent(indent); 2977497057eeSRobert Watson db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 2978497057eeSRobert Watson tp->iss, tp->irs, tp->rcv_nxt); 2979497057eeSRobert Watson 2980497057eeSRobert Watson db_print_indent(indent); 29813ac12506SJonathan T. Looney db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n", 2982497057eeSRobert Watson tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 2983497057eeSRobert Watson 2984497057eeSRobert Watson db_print_indent(indent); 29853ac12506SJonathan T. Looney db_printf("snd_wnd: %u snd_cwnd: %u\n", 29861c18314dSAndre Oppermann tp->snd_wnd, tp->snd_cwnd); 2987497057eeSRobert Watson 2988497057eeSRobert Watson db_print_indent(indent); 29893ac12506SJonathan T. Looney db_printf("snd_ssthresh: %u snd_recover: " 29901c18314dSAndre Oppermann "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 2991497057eeSRobert Watson 2992497057eeSRobert Watson db_print_indent(indent); 29930c39d38dSGleb Smirnoff db_printf("t_rcvtime: %u t_startime: %u\n", 29940c39d38dSGleb Smirnoff tp->t_rcvtime, tp->t_starttime); 2995497057eeSRobert Watson 2996497057eeSRobert Watson db_print_indent(indent); 29971c18314dSAndre Oppermann db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 29981c18314dSAndre Oppermann tp->t_rtttime, tp->t_rtseq); 2999497057eeSRobert Watson 3000497057eeSRobert Watson db_print_indent(indent); 30011c18314dSAndre Oppermann db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 30021c18314dSAndre Oppermann tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 3003497057eeSRobert Watson 3004497057eeSRobert Watson db_print_indent(indent); 3005bd4f9866SMichael Tuexen db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u\n", 3006bd4f9866SMichael Tuexen tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin); 3007497057eeSRobert Watson 3008497057eeSRobert Watson db_print_indent(indent); 300918b83b62SRichard Scheffenegger db_printf("t_rttupdated: %u max_sndwnd: %u t_softerror: %d\n", 3010497057eeSRobert Watson tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 3011497057eeSRobert Watson 3012497057eeSRobert Watson db_print_indent(indent); 3013497057eeSRobert Watson db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 3014497057eeSRobert Watson db_print_toobflags(tp->t_oobflags); 3015497057eeSRobert Watson db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 3016497057eeSRobert Watson 3017497057eeSRobert Watson db_print_indent(indent); 3018497057eeSRobert Watson db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 3019497057eeSRobert Watson tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 3020497057eeSRobert Watson 3021497057eeSRobert Watson db_print_indent(indent); 30229f78a87aSJohn Baldwin db_printf("ts_recent: %u ts_recent_age: %u\n", 30231a553740SAndre Oppermann tp->ts_recent, tp->ts_recent_age); 3024497057eeSRobert Watson 3025497057eeSRobert Watson db_print_indent(indent); 3026497057eeSRobert Watson db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 30273ac12506SJonathan T. Looney "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 3028497057eeSRobert Watson 3029497057eeSRobert Watson db_print_indent(indent); 30303ac12506SJonathan T. Looney db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x " 30319f78a87aSJohn Baldwin "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 3032497057eeSRobert Watson tp->snd_recover_prev, tp->t_badrxtwin); 3033497057eeSRobert Watson 3034497057eeSRobert Watson db_print_indent(indent); 30353529149eSAndre Oppermann db_printf("snd_numholes: %d snd_holes first: %p\n", 30363529149eSAndre Oppermann tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 3037497057eeSRobert Watson 3038497057eeSRobert Watson db_print_indent(indent); 3039a3574665SMichael Tuexen db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n", 3040a3574665SMichael Tuexen tp->snd_fack, tp->rcv_numsacks); 3041497057eeSRobert Watson 3042497057eeSRobert Watson /* Skip sackblks, sackhint. */ 3043497057eeSRobert Watson 3044497057eeSRobert Watson db_print_indent(indent); 3045497057eeSRobert Watson db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 3046497057eeSRobert Watson tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 3047497057eeSRobert Watson } 3048497057eeSRobert Watson 3049497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 3050497057eeSRobert Watson { 3051497057eeSRobert Watson struct tcpcb *tp; 3052497057eeSRobert Watson 3053497057eeSRobert Watson if (!have_addr) { 3054497057eeSRobert Watson db_printf("usage: show tcpcb <addr>\n"); 3055497057eeSRobert Watson return; 3056497057eeSRobert Watson } 3057497057eeSRobert Watson tp = (struct tcpcb *)addr; 3058497057eeSRobert Watson 3059497057eeSRobert Watson db_print_tcpcb(tp, "tcpcb", 0); 3060497057eeSRobert Watson } 3061497057eeSRobert Watson #endif 3062