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 1390*dfc4d218SGleb Smirnoff * by struct sockaddr_in. Call in_pcbconnect() to choose local host address 1391*dfc4d218SGleb Smirnoff * and assign a local port number and install the inpcb into the hash. 1392*dfc4d218SGleb 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); 1405*dfc4d218SGleb Smirnoff error = in_pcbconnect(inp, sin, td->td_ucred, true); 14069e46ff4dSGleb Smirnoff INP_HASH_WUNLOCK(&V_tcbinfo); 1407*dfc4d218SGleb 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); 14363d76be28SGleb Smirnoff struct epoch_tracker et; 1437fb59c426SYoshinobu Inoue int error; 1438fb59c426SYoshinobu Inoue 14398501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1440623dce13SRobert Watson 14413d76be28SGleb Smirnoff NET_EPOCH_ENTER(et); 144276f1499fSGleb Smirnoff INP_HASH_WLOCK(&V_tcbinfo); 1443a9d22cceSGleb Smirnoff error = in6_pcbconnect(inp, sin6, td->td_ucred, true); 1444fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 14453d76be28SGleb Smirnoff NET_EPOCH_EXIT(et); 144676f1499fSGleb Smirnoff if (error != 0) 144776f1499fSGleb Smirnoff return (error); 1448fb59c426SYoshinobu Inoue 1449fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1450fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1451970caf60SBjoern A. Zeeb (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1452fb59c426SYoshinobu Inoue tp->request_r_scale++; 1453fb59c426SYoshinobu Inoue 1454a7e201bbSAndrey V. Elsukov soisconnecting(inp->inp_socket); 145578b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 145657f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 14578e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 14588e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 14598e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1460fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1461fb59c426SYoshinobu Inoue 146276f1499fSGleb Smirnoff return (0); 1463fb59c426SYoshinobu Inoue } 1464fb59c426SYoshinobu Inoue #endif /* INET6 */ 1465fb59c426SYoshinobu Inoue 1466cfe8b629SGarrett Wollman /* 1467b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1468b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1469b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1470b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1471b8af5dfaSRobert Watson * from Linux. 1472b8af5dfaSRobert Watson */ 1473b8af5dfaSRobert Watson static void 1474ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1475b8af5dfaSRobert Watson { 1476b8af5dfaSRobert Watson 14779eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 1478b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1479b8af5dfaSRobert Watson 1480b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1481b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1482b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 14833529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 1484b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1485b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1486b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1487b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1488b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1489b8af5dfaSRobert Watson } 14903f169c54SRichard Scheffenegger if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) 14915a17b6adSMichael Tuexen ti->tcpi_options |= TCPI_OPT_ECN; 14921baaf834SBruce M Simpson 149343d94734SJohn Baldwin ti->tcpi_rto = tp->t_rxtcur * tick; 14943ac12506SJonathan T. Looney ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick; 14951baaf834SBruce M Simpson ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 14961baaf834SBruce M Simpson ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 14971baaf834SBruce M Simpson 1498b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1499b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1500b8af5dfaSRobert Watson 1501b8af5dfaSRobert Watson /* 1502b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1503b8af5dfaSRobert Watson */ 1504c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1505535fbad6SKip Macy ti->tcpi_rcv_nxt = tp->rcv_nxt; 1506b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 15071c18314dSAndre Oppermann ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1508535fbad6SKip Macy ti->tcpi_snd_nxt = tp->snd_nxt; 150943d94734SJohn Baldwin ti->tcpi_snd_mss = tp->t_maxseg; 151043d94734SJohn Baldwin ti->tcpi_rcv_mss = tp->t_maxseg; 1511f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1512f5d34df5SGeorge V. Neville-Neil ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1513f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1514a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD 1515a6456410SNavdeep Parhar if (tp->t_flags & TF_TOE) { 1516a6456410SNavdeep Parhar ti->tcpi_options |= TCPI_OPT_TOE; 1517a6456410SNavdeep Parhar tcp_offload_tcp_info(tp, ti); 1518a6456410SNavdeep Parhar } 1519a6456410SNavdeep Parhar #endif 152022c81cc5SRichard Scheffenegger /* 152122c81cc5SRichard Scheffenegger * AccECN related counters. 152222c81cc5SRichard Scheffenegger */ 152322c81cc5SRichard Scheffenegger if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) == 152422c81cc5SRichard Scheffenegger (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) 152522c81cc5SRichard Scheffenegger /* 152622c81cc5SRichard Scheffenegger * Internal counter starts at 5 for AccECN 152722c81cc5SRichard Scheffenegger * but 0 for RFC3168 ECN. 152822c81cc5SRichard Scheffenegger */ 152922c81cc5SRichard Scheffenegger ti->tcpi_delivered_ce = tp->t_scep - 5; 153022c81cc5SRichard Scheffenegger else 153122c81cc5SRichard Scheffenegger ti->tcpi_delivered_ce = tp->t_scep; 153222c81cc5SRichard Scheffenegger ti->tcpi_received_ce = tp->t_rcep; 1533b8af5dfaSRobert Watson } 1534b8af5dfaSRobert Watson 1535b8af5dfaSRobert Watson /* 15361e8f5ffaSRobert Watson * tcp_ctloutput() must drop the inpcb lock before performing copyin on 15371e8f5ffaSRobert Watson * socket option arguments. When it re-acquires the lock after the copy, it 15381e8f5ffaSRobert Watson * has to revalidate that the connection is still valid for the socket 15391e8f5ffaSRobert Watson * option. 1540cfe8b629SGarrett Wollman */ 1541bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \ 15428501a69cSRobert Watson INP_WLOCK(inp); \ 154353af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { \ 15448501a69cSRobert Watson INP_WUNLOCK(inp); \ 1545bac5bedfSConrad Meyer cleanup; \ 15461e8f5ffaSRobert Watson return (ECONNRESET); \ 15471e8f5ffaSRobert Watson } \ 15481e8f5ffaSRobert Watson tp = intotcpcb(inp); \ 15491e8f5ffaSRobert Watson } while(0) 1550bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */) 15511e8f5ffaSRobert Watson 1552fd7daa72SMichael Tuexen int 1553fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt) 1554df8bae1dSRodney W. Grimes { 1555fd7daa72SMichael Tuexen struct socket *so = inp->inp_socket; 1556fc4d53ccSGleb Smirnoff struct tcpcb *tp = intotcpcb(inp); 1557fc4d53ccSGleb Smirnoff int error = 0; 1558df8bae1dSRodney W. Grimes 1559fc4d53ccSGleb Smirnoff MPASS(sopt->sopt_dir == SOPT_SET); 15603b3c08c1SMichael Tuexen INP_WLOCK_ASSERT(inp); 156153af6903SGleb Smirnoff KASSERT((inp->inp_flags & INP_DROPPED) == 0, 1562fd7daa72SMichael Tuexen ("inp_flags == %x", inp->inp_flags)); 1563fd7daa72SMichael Tuexen KASSERT(so != NULL, ("inp_socket == NULL")); 1564fc4d53ccSGleb Smirnoff 1565cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 15663b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 1567fb59c426SYoshinobu Inoue #ifdef INET6 1568de156263SGleb Smirnoff if (inp->inp_vflag & INP_IPV6PROTO) 1569fd7daa72SMichael Tuexen error = ip6_ctloutput(so, sopt); 1570de156263SGleb Smirnoff #endif 1571de156263SGleb Smirnoff #if defined(INET6) && defined(INET) 1572de156263SGleb Smirnoff else 1573de156263SGleb Smirnoff #endif 1574de156263SGleb Smirnoff #ifdef INET 1575fd7daa72SMichael Tuexen error = ip_ctloutput(so, sopt); 1576de156263SGleb Smirnoff #endif 15775dff1c38SMichael Tuexen /* 1578de156263SGleb Smirnoff * When an IP-level socket option affects TCP, pass control 1579de156263SGleb Smirnoff * down to stack tfb_tcp_ctloutput, otherwise return what 1580de156263SGleb Smirnoff * IP level returned. 15815dff1c38SMichael Tuexen */ 1582de156263SGleb Smirnoff switch (sopt->sopt_level) { 1583de156263SGleb Smirnoff #ifdef INET6 1584de156263SGleb Smirnoff case IPPROTO_IPV6: 1585de156263SGleb Smirnoff if ((inp->inp_vflag & INP_IPV6PROTO) == 0) 1586de156263SGleb Smirnoff return (error); 1587de156263SGleb Smirnoff switch (sopt->sopt_name) { 1588de156263SGleb Smirnoff case IPV6_TCLASS: 1589de156263SGleb Smirnoff /* Notify tcp stacks that care (e.g. RACK). */ 1590de156263SGleb Smirnoff break; 1591de156263SGleb Smirnoff case IPV6_USE_MIN_MTU: 1592f581a26eSGleb Smirnoff /* Update t_maxseg accordingly. */ 1593f581a26eSGleb Smirnoff break; 1594de156263SGleb Smirnoff default: 1595de156263SGleb Smirnoff return (error); 15965dff1c38SMichael Tuexen } 1597de156263SGleb Smirnoff break; 1598b287c6c7SBjoern A. Zeeb #endif 1599b287c6c7SBjoern A. Zeeb #ifdef INET 1600de156263SGleb Smirnoff case IPPROTO_IP: 1601de156263SGleb Smirnoff switch (sopt->sopt_name) { 1602de156263SGleb Smirnoff case IP_TOS: 16033b0ee680SRichard Scheffenegger inp->inp_ip_tos &= ~IPTOS_ECN_MASK; 16043b0ee680SRichard Scheffenegger break; 1605de156263SGleb Smirnoff case IP_TTL: 1606de156263SGleb Smirnoff /* Notify tcp stacks that care (e.g. RACK). */ 1607de156263SGleb Smirnoff break; 1608de156263SGleb Smirnoff default: 1609df8bae1dSRodney W. Grimes return (error); 1610de156263SGleb Smirnoff } 1611de156263SGleb Smirnoff break; 1612de156263SGleb Smirnoff #endif 1613de156263SGleb Smirnoff default: 1614de156263SGleb Smirnoff return (error); 1615de156263SGleb Smirnoff } 16163b3c08c1SMichael Tuexen INP_WLOCK(inp); 161753af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 16183b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 16193b3c08c1SMichael Tuexen return (ECONNRESET); 16203b3c08c1SMichael Tuexen } 1621fc4d53ccSGleb Smirnoff } else if (sopt->sopt_name == TCP_FUNCTION_BLK) { 1622fc4d53ccSGleb Smirnoff /* 1623fc4d53ccSGleb Smirnoff * Protect the TCP option TCP_FUNCTION_BLK so 1624fc4d53ccSGleb Smirnoff * that a sub-function can *never* overwrite this. 1625fc4d53ccSGleb Smirnoff */ 1626fc4d53ccSGleb Smirnoff struct tcp_function_set fsn; 1627fc4d53ccSGleb Smirnoff struct tcp_function_block *blk; 1628fc4d53ccSGleb Smirnoff 16293b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 1630fc4d53ccSGleb Smirnoff error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn); 1631fc4d53ccSGleb Smirnoff if (error) 1632fc4d53ccSGleb Smirnoff return (error); 1633fc4d53ccSGleb Smirnoff 163468cea2b1SJohn Baldwin INP_WLOCK(inp); 163553af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 16368501a69cSRobert Watson INP_WUNLOCK(inp); 16371e8f5ffaSRobert Watson return (ECONNRESET); 1638623dce13SRobert Watson } 163955bceb1eSRandall Stewart tp = intotcpcb(inp); 1640fc4d53ccSGleb Smirnoff 164155bceb1eSRandall Stewart blk = find_and_ref_tcp_functions(&fsn); 164255bceb1eSRandall Stewart if (blk == NULL) { 164355bceb1eSRandall Stewart INP_WUNLOCK(inp); 164455bceb1eSRandall Stewart return (ENOENT); 164555bceb1eSRandall Stewart } 1646587d67c0SRandall Stewart if (tp->t_fb == blk) { 1647587d67c0SRandall Stewart /* You already have this */ 1648587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1649587d67c0SRandall Stewart INP_WUNLOCK(inp); 1650587d67c0SRandall Stewart return (0); 1651587d67c0SRandall Stewart } 1652587d67c0SRandall Stewart if (tp->t_state != TCPS_CLOSED) { 1653587d67c0SRandall Stewart /* 1654587d67c0SRandall Stewart * The user has advanced the state 1655587d67c0SRandall Stewart * past the initial point, we may not 1656587d67c0SRandall Stewart * be able to switch. 1657587d67c0SRandall Stewart */ 1658587d67c0SRandall Stewart if (blk->tfb_tcp_handoff_ok != NULL) { 1659587d67c0SRandall Stewart /* 1660587d67c0SRandall Stewart * Does the stack provide a 1661587d67c0SRandall Stewart * query mechanism, if so it may 1662587d67c0SRandall Stewart * still be possible? 1663587d67c0SRandall Stewart */ 1664587d67c0SRandall Stewart error = (*blk->tfb_tcp_handoff_ok)(tp); 1665c6c0be27SMichael Tuexen } else 1666c6c0be27SMichael Tuexen error = EINVAL; 1667587d67c0SRandall Stewart if (error) { 1668587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1669587d67c0SRandall Stewart INP_WUNLOCK(inp); 1670587d67c0SRandall Stewart return(error); 1671587d67c0SRandall Stewart } 1672587d67c0SRandall Stewart } 167355bceb1eSRandall Stewart if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) { 167455bceb1eSRandall Stewart refcount_release(&blk->tfb_refcnt); 167555bceb1eSRandall Stewart INP_WUNLOCK(inp); 167655bceb1eSRandall Stewart return (ENOENT); 167755bceb1eSRandall Stewart } 167855bceb1eSRandall Stewart /* 167955bceb1eSRandall Stewart * Release the old refcnt, the 1680587d67c0SRandall Stewart * lookup acquired a ref on the 1681587d67c0SRandall Stewart * new one already. 168255bceb1eSRandall Stewart */ 1683587d67c0SRandall Stewart if (tp->t_fb->tfb_tcp_fb_fini) { 1684086a3556SAndrew Gallatin struct epoch_tracker et; 1685587d67c0SRandall Stewart /* 1686587d67c0SRandall Stewart * Tell the stack to cleanup with 0 i.e. 1687587d67c0SRandall Stewart * the tcb is not going away. 1688587d67c0SRandall Stewart */ 1689086a3556SAndrew Gallatin NET_EPOCH_ENTER(et); 1690587d67c0SRandall Stewart (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0); 1691086a3556SAndrew Gallatin NET_EPOCH_EXIT(et); 1692587d67c0SRandall Stewart } 16933ee9c3c4SRandall Stewart #ifdef TCPHPTS 16943ee9c3c4SRandall Stewart /* Assure that we are not on any hpts */ 16959eb0e832SGleb Smirnoff tcp_hpts_remove(tptoinpcb(tp)); 16963ee9c3c4SRandall Stewart #endif 16973ee9c3c4SRandall Stewart if (blk->tfb_tcp_fb_init) { 16983ee9c3c4SRandall Stewart error = (*blk->tfb_tcp_fb_init)(tp); 16993ee9c3c4SRandall Stewart if (error) { 17003ee9c3c4SRandall Stewart refcount_release(&blk->tfb_refcnt); 17013ee9c3c4SRandall Stewart if (tp->t_fb->tfb_tcp_fb_init) { 17023ee9c3c4SRandall Stewart if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) { 17033ee9c3c4SRandall Stewart /* Fall back failed, drop the connection */ 17043ee9c3c4SRandall Stewart INP_WUNLOCK(inp); 1705fd7daa72SMichael Tuexen soabort(so); 17063ee9c3c4SRandall Stewart return (error); 17073ee9c3c4SRandall Stewart } 17083ee9c3c4SRandall Stewart } 17093ee9c3c4SRandall Stewart goto err_out; 17103ee9c3c4SRandall Stewart } 17113ee9c3c4SRandall Stewart } 171255bceb1eSRandall Stewart refcount_release(&tp->t_fb->tfb_refcnt); 171355bceb1eSRandall Stewart tp->t_fb = blk; 171455bceb1eSRandall Stewart #ifdef TCP_OFFLOAD 171555bceb1eSRandall Stewart if (tp->t_flags & TF_TOE) { 171655bceb1eSRandall Stewart tcp_offload_ctloutput(tp, sopt->sopt_dir, 171755bceb1eSRandall Stewart sopt->sopt_name); 171855bceb1eSRandall Stewart } 171955bceb1eSRandall Stewart #endif 17203ee9c3c4SRandall Stewart err_out: 172155bceb1eSRandall Stewart INP_WUNLOCK(inp); 172255bceb1eSRandall Stewart return (error); 1723fc4d53ccSGleb Smirnoff } 1724fc4d53ccSGleb Smirnoff 17253b3c08c1SMichael Tuexen /* Pass in the INP locked, callee must unlock it. */ 17263b3c08c1SMichael Tuexen return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt)); 1727fc4d53ccSGleb Smirnoff } 1728fc4d53ccSGleb Smirnoff 1729fc4d53ccSGleb Smirnoff static int 1730fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt) 1731fc4d53ccSGleb Smirnoff { 1732fd7daa72SMichael Tuexen struct socket *so = inp->inp_socket; 1733fd7daa72SMichael Tuexen struct tcpcb *tp = intotcpcb(inp); 1734fc4d53ccSGleb Smirnoff int error = 0; 1735fc4d53ccSGleb Smirnoff 1736fc4d53ccSGleb Smirnoff MPASS(sopt->sopt_dir == SOPT_GET); 17373b3c08c1SMichael Tuexen INP_WLOCK_ASSERT(inp); 173853af6903SGleb Smirnoff KASSERT((inp->inp_flags & INP_DROPPED) == 0, 1739fd7daa72SMichael Tuexen ("inp_flags == %x", inp->inp_flags)); 1740fd7daa72SMichael Tuexen KASSERT(so != NULL, ("inp_socket == NULL")); 1741fc4d53ccSGleb Smirnoff 1742fc4d53ccSGleb Smirnoff if (sopt->sopt_level != IPPROTO_TCP) { 17433b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 1744fc4d53ccSGleb Smirnoff #ifdef INET6 1745fc4d53ccSGleb Smirnoff if (inp->inp_vflag & INP_IPV6PROTO) 1746fd7daa72SMichael Tuexen error = ip6_ctloutput(so, sopt); 1747fc4d53ccSGleb Smirnoff #endif /* INET6 */ 1748fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET) 1749fc4d53ccSGleb Smirnoff else 1750fc4d53ccSGleb Smirnoff #endif 1751fc4d53ccSGleb Smirnoff #ifdef INET 1752fd7daa72SMichael Tuexen error = ip_ctloutput(so, sopt); 1753fc4d53ccSGleb Smirnoff #endif 1754fc4d53ccSGleb Smirnoff return (error); 1755fc4d53ccSGleb Smirnoff } 1756fc4d53ccSGleb Smirnoff if (((sopt->sopt_name == TCP_FUNCTION_BLK) || 1757e2833083SPeter Lei (sopt->sopt_name == TCP_FUNCTION_ALIAS))) { 1758fc4d53ccSGleb Smirnoff struct tcp_function_set fsn; 1759fc4d53ccSGleb Smirnoff 1760e2833083SPeter Lei if (sopt->sopt_name == TCP_FUNCTION_ALIAS) { 1761e2833083SPeter Lei memset(&fsn, 0, sizeof(fsn)); 1762e2833083SPeter Lei find_tcp_function_alias(tp->t_fb, &fsn); 1763e2833083SPeter Lei } else { 1764e2833083SPeter Lei strncpy(fsn.function_set_name, 1765e2833083SPeter Lei tp->t_fb->tfb_tcp_block_name, 1766c73b6f4dSEd Maste TCP_FUNCTION_NAME_LEN_MAX); 1767c73b6f4dSEd Maste fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0'; 1768e2833083SPeter Lei } 176955bceb1eSRandall Stewart fsn.pcbcnt = tp->t_fb->tfb_refcnt; 177055bceb1eSRandall Stewart INP_WUNLOCK(inp); 177155bceb1eSRandall Stewart error = sooptcopyout(sopt, &fsn, sizeof fsn); 177255bceb1eSRandall Stewart return (error); 177355bceb1eSRandall Stewart } 1774fc4d53ccSGleb Smirnoff 17753b3c08c1SMichael Tuexen /* Pass in the INP locked, callee must unlock it. */ 17763b3c08c1SMichael Tuexen return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt)); 1777fc4d53ccSGleb Smirnoff } 1778fc4d53ccSGleb Smirnoff 1779fc4d53ccSGleb Smirnoff int 1780fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1781fc4d53ccSGleb Smirnoff { 1782fc4d53ccSGleb Smirnoff struct inpcb *inp; 1783fc4d53ccSGleb Smirnoff 1784fc4d53ccSGleb Smirnoff inp = sotoinpcb(so); 1785fc4d53ccSGleb Smirnoff KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1786fc4d53ccSGleb Smirnoff 17873b3c08c1SMichael Tuexen INP_WLOCK(inp); 178853af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 17893b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 17903b3c08c1SMichael Tuexen return (ECONNRESET); 17913b3c08c1SMichael Tuexen } 1792fc4d53ccSGleb Smirnoff if (sopt->sopt_dir == SOPT_SET) 1793fc4d53ccSGleb Smirnoff return (tcp_ctloutput_set(inp, sopt)); 1794fc4d53ccSGleb Smirnoff else if (sopt->sopt_dir == SOPT_GET) 1795fc4d53ccSGleb Smirnoff return (tcp_ctloutput_get(inp, sopt)); 1796fc4d53ccSGleb Smirnoff else 1797fc4d53ccSGleb Smirnoff panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir); 179855bceb1eSRandall Stewart } 179955bceb1eSRandall Stewart 18002529f56eSJonathan T. Looney /* 18012529f56eSJonathan T. Looney * If this assert becomes untrue, we need to change the size of the buf 18022529f56eSJonathan T. Looney * variable in tcp_default_ctloutput(). 18032529f56eSJonathan T. Looney */ 18042529f56eSJonathan T. Looney #ifdef CTASSERT 18052529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN); 18062529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN); 18072529f56eSJonathan T. Looney #endif 18082529f56eSJonathan T. Looney 1809ec1db6e1SJohn Baldwin #ifdef KERN_TLS 1810ec1db6e1SJohn Baldwin static int 1811ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls) 1812ec1db6e1SJohn Baldwin { 1813ec1db6e1SJohn Baldwin struct tls_enable_v0 tls_v0; 1814ec1db6e1SJohn Baldwin int error; 1815ec1db6e1SJohn Baldwin 1816ec1db6e1SJohn Baldwin if (sopt->sopt_valsize == sizeof(tls_v0)) { 1817ec1db6e1SJohn Baldwin error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0), 1818ec1db6e1SJohn Baldwin sizeof(tls_v0)); 1819ec1db6e1SJohn Baldwin if (error) 1820ec1db6e1SJohn Baldwin return (error); 1821ec1db6e1SJohn Baldwin memset(tls, 0, sizeof(*tls)); 1822ec1db6e1SJohn Baldwin tls->cipher_key = tls_v0.cipher_key; 1823ec1db6e1SJohn Baldwin tls->iv = tls_v0.iv; 1824ec1db6e1SJohn Baldwin tls->auth_key = tls_v0.auth_key; 1825ec1db6e1SJohn Baldwin tls->cipher_algorithm = tls_v0.cipher_algorithm; 1826ec1db6e1SJohn Baldwin tls->cipher_key_len = tls_v0.cipher_key_len; 1827ec1db6e1SJohn Baldwin tls->iv_len = tls_v0.iv_len; 1828ec1db6e1SJohn Baldwin tls->auth_algorithm = tls_v0.auth_algorithm; 1829ec1db6e1SJohn Baldwin tls->auth_key_len = tls_v0.auth_key_len; 1830ec1db6e1SJohn Baldwin tls->flags = tls_v0.flags; 1831ec1db6e1SJohn Baldwin tls->tls_vmajor = tls_v0.tls_vmajor; 1832ec1db6e1SJohn Baldwin tls->tls_vminor = tls_v0.tls_vminor; 1833ec1db6e1SJohn Baldwin return (0); 1834ec1db6e1SJohn Baldwin } 1835ec1db6e1SJohn Baldwin 1836ec1db6e1SJohn Baldwin return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls))); 1837ec1db6e1SJohn Baldwin } 1838ec1db6e1SJohn Baldwin #endif 1839ec1db6e1SJohn Baldwin 1840b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo; 1841b8d60729SRandall Stewart 1842b8d60729SRandall Stewart static int 1843ea9017fbSRandall Stewart tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt) 1844b8d60729SRandall Stewart { 1845b8d60729SRandall Stewart struct cc_algo *algo; 1846b8d60729SRandall Stewart void *ptr = NULL; 18473b3c08c1SMichael Tuexen struct tcpcb *tp; 1848b8d60729SRandall Stewart struct cc_var cc_mem; 1849b8d60729SRandall Stewart char buf[TCP_CA_NAME_MAX]; 1850b8d60729SRandall Stewart size_t mem_sz; 1851b8d60729SRandall Stewart int error; 1852b8d60729SRandall Stewart 1853b8d60729SRandall Stewart INP_WUNLOCK(inp); 1854b8d60729SRandall Stewart error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1); 1855b8d60729SRandall Stewart if (error) 1856b8d60729SRandall Stewart return(error); 1857b8d60729SRandall Stewart buf[sopt->sopt_valsize] = '\0'; 1858b8d60729SRandall Stewart CC_LIST_RLOCK(); 1859ea9017fbSRandall Stewart STAILQ_FOREACH(algo, &cc_list, entries) { 1860b8d60729SRandall Stewart if (strncmp(buf, algo->name, 1861b8d60729SRandall Stewart TCP_CA_NAME_MAX) == 0) { 1862b8d60729SRandall Stewart if (algo->flags & CC_MODULE_BEING_REMOVED) { 1863b8d60729SRandall Stewart /* We can't "see" modules being unloaded */ 1864b8d60729SRandall Stewart continue; 1865b8d60729SRandall Stewart } 1866b8d60729SRandall Stewart break; 1867b8d60729SRandall Stewart } 1868ea9017fbSRandall Stewart } 1869b8d60729SRandall Stewart if (algo == NULL) { 1870b8d60729SRandall Stewart CC_LIST_RUNLOCK(); 1871b8d60729SRandall Stewart return(ESRCH); 1872b8d60729SRandall Stewart } 1873ea9017fbSRandall Stewart /* 1874ea9017fbSRandall Stewart * With a reference the algorithm cannot be removed 1875ea9017fbSRandall Stewart * so we hold a reference through the change process. 1876ea9017fbSRandall Stewart */ 1877ea9017fbSRandall Stewart cc_refer(algo); 1878ea9017fbSRandall Stewart CC_LIST_RUNLOCK(); 1879b8d60729SRandall Stewart if (algo->cb_init != NULL) { 1880b8d60729SRandall Stewart /* We can now pre-get the memory for the CC */ 1881b8d60729SRandall Stewart mem_sz = (*algo->cc_data_sz)(); 1882b8d60729SRandall Stewart if (mem_sz == 0) { 1883b8d60729SRandall Stewart goto no_mem_needed; 1884b8d60729SRandall Stewart } 1885b8d60729SRandall Stewart ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK); 1886b8d60729SRandall Stewart } else { 1887b8d60729SRandall Stewart no_mem_needed: 1888b8d60729SRandall Stewart mem_sz = 0; 1889b8d60729SRandall Stewart ptr = NULL; 1890b8d60729SRandall Stewart } 1891b8d60729SRandall Stewart /* 1892b8d60729SRandall Stewart * Make sure its all clean and zero and also get 1893b8d60729SRandall Stewart * back the inplock. 1894b8d60729SRandall Stewart */ 1895b8d60729SRandall Stewart memset(&cc_mem, 0, sizeof(cc_mem)); 1896df07bfdaSMichael Tuexen INP_WLOCK(inp); 189753af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 1898df07bfdaSMichael Tuexen INP_WUNLOCK(inp); 1899ea9017fbSRandall Stewart if (ptr) 1900df07bfdaSMichael Tuexen free(ptr, M_CC_MEM); 1901ea9017fbSRandall Stewart /* Release our temp reference */ 1902ea9017fbSRandall Stewart CC_LIST_RLOCK(); 1903ea9017fbSRandall Stewart cc_release(algo); 1904ea9017fbSRandall Stewart CC_LIST_RUNLOCK(); 1905df07bfdaSMichael Tuexen return (ECONNRESET); 1906df07bfdaSMichael Tuexen } 1907df07bfdaSMichael Tuexen tp = intotcpcb(inp); 1908df07bfdaSMichael Tuexen if (ptr != NULL) 1909b8d60729SRandall Stewart memset(ptr, 0, mem_sz); 1910b8d60729SRandall Stewart cc_mem.ccvc.tcp = tp; 1911b8d60729SRandall Stewart /* 1912b8d60729SRandall Stewart * We once again hold a write lock over the tcb so it's 1913b8d60729SRandall Stewart * safe to do these things without ordering concerns. 1914b8d60729SRandall Stewart * Note here we init into stack memory. 1915b8d60729SRandall Stewart */ 1916b8d60729SRandall Stewart if (algo->cb_init != NULL) 1917b8d60729SRandall Stewart error = algo->cb_init(&cc_mem, ptr); 1918b8d60729SRandall Stewart else 1919b8d60729SRandall Stewart error = 0; 1920b8d60729SRandall Stewart /* 1921b8d60729SRandall Stewart * The CC algorithms, when given their memory 1922b8d60729SRandall Stewart * should not fail we could in theory have a 1923b8d60729SRandall Stewart * KASSERT here. 1924b8d60729SRandall Stewart */ 1925b8d60729SRandall Stewart if (error == 0) { 1926b8d60729SRandall Stewart /* 1927b8d60729SRandall Stewart * Touchdown, lets go ahead and move the 1928b8d60729SRandall Stewart * connection to the new CC module by 1929b8d60729SRandall Stewart * copying in the cc_mem after we call 1930b8d60729SRandall Stewart * the old ones cleanup (if any). 1931b8d60729SRandall Stewart */ 1932b8d60729SRandall Stewart if (CC_ALGO(tp)->cb_destroy != NULL) 1933e68b3792SGleb Smirnoff CC_ALGO(tp)->cb_destroy(&tp->t_ccv); 1934ea9017fbSRandall Stewart /* Detach the old CC from the tcpcb */ 1935ea9017fbSRandall Stewart cc_detach(tp); 1936ea9017fbSRandall Stewart /* Copy in our temp memory that was inited */ 1937e68b3792SGleb Smirnoff memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var)); 1938ea9017fbSRandall Stewart /* Now attach the new, which takes a reference */ 1939ea9017fbSRandall Stewart cc_attach(tp, algo); 1940b8d60729SRandall Stewart /* Ok now are we where we have gotten past any conn_init? */ 1941b8d60729SRandall Stewart if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) { 1942b8d60729SRandall Stewart /* Yep run the connection init for the new CC */ 1943e68b3792SGleb Smirnoff CC_ALGO(tp)->conn_init(&tp->t_ccv); 1944b8d60729SRandall Stewart } 1945b8d60729SRandall Stewart } else if (ptr) 1946b8d60729SRandall Stewart free(ptr, M_CC_MEM); 1947b8d60729SRandall Stewart INP_WUNLOCK(inp); 1948ea9017fbSRandall Stewart /* Now lets release our temp reference */ 1949ea9017fbSRandall Stewart CC_LIST_RLOCK(); 1950ea9017fbSRandall Stewart cc_release(algo); 1951ea9017fbSRandall Stewart CC_LIST_RUNLOCK(); 1952b8d60729SRandall Stewart return (error); 1953b8d60729SRandall Stewart } 1954b8d60729SRandall Stewart 195555bceb1eSRandall Stewart int 19563b3c08c1SMichael Tuexen tcp_default_ctloutput(struct inpcb *inp, struct sockopt *sopt) 195755bceb1eSRandall Stewart { 1958fd7daa72SMichael Tuexen struct tcpcb *tp = intotcpcb(inp); 195955bceb1eSRandall Stewart int error, opt, optval; 196055bceb1eSRandall Stewart u_int ui; 196155bceb1eSRandall Stewart struct tcp_info ti; 1962b2e60773SJohn Baldwin #ifdef KERN_TLS 1963b2e60773SJohn Baldwin struct tls_enable tls; 1964528c7649SMichael Tuexen struct socket *so = inp->inp_socket; 1965b2e60773SJohn Baldwin #endif 19662529f56eSJonathan T. Looney char *pbuf, buf[TCP_LOG_ID_LEN]; 1967adc56f5aSEdward Tomasz Napierala #ifdef STATS 1968adc56f5aSEdward Tomasz Napierala struct statsblob *sbp; 1969adc56f5aSEdward Tomasz Napierala #endif 1970af6fef3aSGleb Smirnoff size_t len; 1971df8bae1dSRodney W. Grimes 1972f581a26eSGleb Smirnoff INP_WLOCK_ASSERT(inp); 197353af6903SGleb Smirnoff KASSERT((inp->inp_flags & INP_DROPPED) == 0, 1974fd7daa72SMichael Tuexen ("inp_flags == %x", inp->inp_flags)); 1975528c7649SMichael Tuexen KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL")); 1976f581a26eSGleb Smirnoff 1977f581a26eSGleb Smirnoff switch (sopt->sopt_level) { 1978f581a26eSGleb Smirnoff #ifdef INET6 1979f581a26eSGleb Smirnoff case IPPROTO_IPV6: 1980f581a26eSGleb Smirnoff MPASS(inp->inp_vflag & INP_IPV6PROTO); 1981f581a26eSGleb Smirnoff switch (sopt->sopt_name) { 1982f581a26eSGleb Smirnoff case IPV6_USE_MIN_MTU: 1983f581a26eSGleb Smirnoff tcp6_use_min_mtu(tp); 1984f581a26eSGleb Smirnoff /* FALLTHROUGH */ 1985f581a26eSGleb Smirnoff } 1986f581a26eSGleb Smirnoff INP_WUNLOCK(inp); 1987f581a26eSGleb Smirnoff return (0); 1988f581a26eSGleb Smirnoff #endif 1989f581a26eSGleb Smirnoff #ifdef INET 1990f581a26eSGleb Smirnoff case IPPROTO_IP: 1991f581a26eSGleb Smirnoff INP_WUNLOCK(inp); 1992f581a26eSGleb Smirnoff return (0); 1993f581a26eSGleb Smirnoff #endif 1994f581a26eSGleb Smirnoff } 1995f581a26eSGleb Smirnoff 1996d519cedbSGleb Smirnoff /* 1997d519cedbSGleb Smirnoff * For TCP_CCALGOOPT forward the control to CC module, for both 1998d519cedbSGleb Smirnoff * SOPT_SET and SOPT_GET. 1999d519cedbSGleb Smirnoff */ 2000d519cedbSGleb Smirnoff switch (sopt->sopt_name) { 2001d519cedbSGleb Smirnoff case TCP_CCALGOOPT: 2002d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 2003c8b53cedSMichael Tuexen if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT) 2004c8b53cedSMichael Tuexen return (EINVAL); 2005af6fef3aSGleb Smirnoff pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO); 2006af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize, 2007d519cedbSGleb Smirnoff sopt->sopt_valsize); 2008d519cedbSGleb Smirnoff if (error) { 2009af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 2010d519cedbSGleb Smirnoff return (error); 2011d519cedbSGleb Smirnoff } 2012bac5bedfSConrad Meyer INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP)); 2013d519cedbSGleb Smirnoff if (CC_ALGO(tp)->ctl_output != NULL) 2014e68b3792SGleb Smirnoff error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf); 2015d519cedbSGleb Smirnoff else 2016d519cedbSGleb Smirnoff error = ENOENT; 2017d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 2018d519cedbSGleb Smirnoff if (error == 0 && sopt->sopt_dir == SOPT_GET) 2019af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize); 2020af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 2021d519cedbSGleb Smirnoff return (error); 2022d519cedbSGleb Smirnoff } 2023d519cedbSGleb Smirnoff 2024cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 2025cfe8b629SGarrett Wollman case SOPT_SET: 2026cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2027fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 202888f6b043SBruce M Simpson case TCP_MD5SIG: 20298501a69cSRobert Watson INP_WUNLOCK(inp); 203097453e5eSClaudio Jeker if (!TCPMD5_ENABLED()) 2031fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 2032fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 20331cfd4b53SBruce M Simpson if (error) 20341e8f5ffaSRobert Watson return (error); 203597453e5eSClaudio Jeker INP_WLOCK_RECHECK(inp); 203609fe6320SNavdeep Parhar goto unlock_and_done; 2037fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 203809fe6320SNavdeep Parhar 2039df8bae1dSRodney W. Grimes case TCP_NODELAY: 2040cfe8b629SGarrett Wollman case TCP_NOOPT: 20410471a8c7SRichard Scheffenegger case TCP_LRD: 20428501a69cSRobert Watson INP_WUNLOCK(inp); 2043cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 2044cfe8b629SGarrett Wollman sizeof optval); 2045cfe8b629SGarrett Wollman if (error) 20461e8f5ffaSRobert Watson return (error); 2047cfe8b629SGarrett Wollman 20488501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 2049cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2050cfe8b629SGarrett Wollman case TCP_NODELAY: 2051cfe8b629SGarrett Wollman opt = TF_NODELAY; 2052cfe8b629SGarrett Wollman break; 2053cfe8b629SGarrett Wollman case TCP_NOOPT: 2054cfe8b629SGarrett Wollman opt = TF_NOOPT; 2055cfe8b629SGarrett Wollman break; 20560471a8c7SRichard Scheffenegger case TCP_LRD: 20570471a8c7SRichard Scheffenegger opt = TF_LRD; 20580471a8c7SRichard Scheffenegger break; 2059cfe8b629SGarrett Wollman default: 2060cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 2061cfe8b629SGarrett Wollman break; 2062cfe8b629SGarrett Wollman } 2063cfe8b629SGarrett Wollman 2064cfe8b629SGarrett Wollman if (optval) 2065cfe8b629SGarrett Wollman tp->t_flags |= opt; 2066df8bae1dSRodney W. Grimes else 2067cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 206809fe6320SNavdeep Parhar unlock_and_done: 206909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 207009fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 207109fe6320SNavdeep Parhar tcp_offload_ctloutput(tp, sopt->sopt_dir, 207209fe6320SNavdeep Parhar sopt->sopt_name); 207309fe6320SNavdeep Parhar } 207409fe6320SNavdeep Parhar #endif 20758501a69cSRobert Watson INP_WUNLOCK(inp); 2076df8bae1dSRodney W. Grimes break; 2077df8bae1dSRodney W. Grimes 2078007581c0SJonathan Lemon case TCP_NOPUSH: 20798501a69cSRobert Watson INP_WUNLOCK(inp); 2080007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 2081007581c0SJonathan Lemon sizeof optval); 2082007581c0SJonathan Lemon if (error) 20831e8f5ffaSRobert Watson return (error); 2084007581c0SJonathan Lemon 20858501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 2086007581c0SJonathan Lemon if (optval) 2087007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 2088d28b9e89SJohn Baldwin else if (tp->t_flags & TF_NOPUSH) { 2089007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 2090109eb549SGleb Smirnoff if (TCPS_HAVEESTABLISHED(tp->t_state)) { 2091109eb549SGleb Smirnoff struct epoch_tracker et; 2092109eb549SGleb Smirnoff 2093109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 2094f64dc2abSGleb Smirnoff error = tcp_output_nodrop(tp); 2095109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 2096109eb549SGleb Smirnoff } 2097007581c0SJonathan Lemon } 209809fe6320SNavdeep Parhar goto unlock_and_done; 2099007581c0SJonathan Lemon 21009e644c23SMichael Tuexen case TCP_REMOTE_UDP_ENCAPS_PORT: 21019e644c23SMichael Tuexen INP_WUNLOCK(inp); 21029e644c23SMichael Tuexen error = sooptcopyin(sopt, &optval, sizeof optval, 21039e644c23SMichael Tuexen sizeof optval); 21049e644c23SMichael Tuexen if (error) 21059e644c23SMichael Tuexen return (error); 21069e644c23SMichael Tuexen if ((optval < TCP_TUNNELING_PORT_MIN) || 21079e644c23SMichael Tuexen (optval > TCP_TUNNELING_PORT_MAX)) { 21089e644c23SMichael Tuexen /* Its got to be in range */ 21099e644c23SMichael Tuexen return (EINVAL); 21109e644c23SMichael Tuexen } 21119e644c23SMichael Tuexen if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) { 21129e644c23SMichael Tuexen /* You have to have enabled a UDP tunneling port first */ 21139e644c23SMichael Tuexen return (EINVAL); 21149e644c23SMichael Tuexen } 21159e644c23SMichael Tuexen INP_WLOCK_RECHECK(inp); 21169e644c23SMichael Tuexen if (tp->t_state != TCPS_CLOSED) { 21179e644c23SMichael Tuexen /* You can't change after you are connected */ 21189e644c23SMichael Tuexen error = EINVAL; 21199e644c23SMichael Tuexen } else { 21209e644c23SMichael Tuexen /* Ok we are all good set the port */ 21219e644c23SMichael Tuexen tp->t_port = htons(optval); 21229e644c23SMichael Tuexen } 21239e644c23SMichael Tuexen goto unlock_and_done; 21249e644c23SMichael Tuexen 2125df8bae1dSRodney W. Grimes case TCP_MAXSEG: 21268501a69cSRobert Watson INP_WUNLOCK(inp); 2127cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 2128cfe8b629SGarrett Wollman sizeof optval); 2129cfe8b629SGarrett Wollman if (error) 21301e8f5ffaSRobert Watson return (error); 2131df8bae1dSRodney W. Grimes 21328501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 213353369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 2134603724d3SBjoern A. Zeeb optval + 40 >= V_tcp_minmss) 2135cfe8b629SGarrett Wollman tp->t_maxseg = optval; 2136a0292f23SGarrett Wollman else 2137a0292f23SGarrett Wollman error = EINVAL; 213809fe6320SNavdeep Parhar goto unlock_and_done; 2139a0292f23SGarrett Wollman 2140b8af5dfaSRobert Watson case TCP_INFO: 21418501a69cSRobert Watson INP_WUNLOCK(inp); 2142b8af5dfaSRobert Watson error = EINVAL; 2143b8af5dfaSRobert Watson break; 2144b8af5dfaSRobert Watson 2145adc56f5aSEdward Tomasz Napierala case TCP_STATS: 2146adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2147adc56f5aSEdward Tomasz Napierala #ifdef STATS 2148adc56f5aSEdward Tomasz Napierala error = sooptcopyin(sopt, &optval, sizeof optval, 2149adc56f5aSEdward Tomasz Napierala sizeof optval); 2150adc56f5aSEdward Tomasz Napierala if (error) 2151adc56f5aSEdward Tomasz Napierala return (error); 2152adc56f5aSEdward Tomasz Napierala 2153adc56f5aSEdward Tomasz Napierala if (optval > 0) 2154adc56f5aSEdward Tomasz Napierala sbp = stats_blob_alloc( 2155adc56f5aSEdward Tomasz Napierala V_tcp_perconn_stats_dflt_tpl, 0); 2156adc56f5aSEdward Tomasz Napierala else 2157adc56f5aSEdward Tomasz Napierala sbp = NULL; 2158adc56f5aSEdward Tomasz Napierala 2159adc56f5aSEdward Tomasz Napierala INP_WLOCK_RECHECK(inp); 2160adc56f5aSEdward Tomasz Napierala if ((tp->t_stats != NULL && sbp == NULL) || 2161adc56f5aSEdward Tomasz Napierala (tp->t_stats == NULL && sbp != NULL)) { 2162adc56f5aSEdward Tomasz Napierala struct statsblob *t = tp->t_stats; 2163adc56f5aSEdward Tomasz Napierala tp->t_stats = sbp; 2164adc56f5aSEdward Tomasz Napierala sbp = t; 2165adc56f5aSEdward Tomasz Napierala } 2166adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2167adc56f5aSEdward Tomasz Napierala 2168adc56f5aSEdward Tomasz Napierala stats_blob_destroy(sbp); 2169adc56f5aSEdward Tomasz Napierala #else 2170adc56f5aSEdward Tomasz Napierala return (EOPNOTSUPP); 2171adc56f5aSEdward Tomasz Napierala #endif /* !STATS */ 2172adc56f5aSEdward Tomasz Napierala break; 2173adc56f5aSEdward Tomasz Napierala 2174dbc42409SLawrence Stewart case TCP_CONGESTION: 2175ea9017fbSRandall Stewart error = tcp_set_cc_mod(inp, sopt); 217673e263b1SGleb Smirnoff break; 2177dbc42409SLawrence Stewart 2178a034518aSAndrew Gallatin case TCP_REUSPORT_LB_NUMA: 2179a034518aSAndrew Gallatin INP_WUNLOCK(inp); 2180a034518aSAndrew Gallatin error = sooptcopyin(sopt, &optval, sizeof(optval), 2181a034518aSAndrew Gallatin sizeof(optval)); 2182a034518aSAndrew Gallatin INP_WLOCK_RECHECK(inp); 2183a034518aSAndrew Gallatin if (!error) 2184a034518aSAndrew Gallatin error = in_pcblbgroup_numa(inp, optval); 2185a034518aSAndrew Gallatin INP_WUNLOCK(inp); 2186a034518aSAndrew Gallatin break; 2187a034518aSAndrew Gallatin 2188b2e60773SJohn Baldwin #ifdef KERN_TLS 2189b2e60773SJohn Baldwin case TCP_TXTLS_ENABLE: 2190b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2191ec1db6e1SJohn Baldwin error = copyin_tls_enable(sopt, &tls); 2192b2e60773SJohn Baldwin if (error) 2193b2e60773SJohn Baldwin break; 2194fd7daa72SMichael Tuexen error = ktls_enable_tx(so, &tls); 2195b2e60773SJohn Baldwin break; 2196b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2197b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2198b2e60773SJohn Baldwin error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 2199b2e60773SJohn Baldwin if (error) 2200b2e60773SJohn Baldwin return (error); 2201b2e60773SJohn Baldwin 2202b2e60773SJohn Baldwin INP_WLOCK_RECHECK(inp); 2203fd7daa72SMichael Tuexen error = ktls_set_tx_mode(so, ui); 2204b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2205b2e60773SJohn Baldwin break; 2206f1f93475SJohn Baldwin case TCP_RXTLS_ENABLE: 2207f1f93475SJohn Baldwin INP_WUNLOCK(inp); 2208f1f93475SJohn Baldwin error = sooptcopyin(sopt, &tls, sizeof(tls), 2209f1f93475SJohn Baldwin sizeof(tls)); 2210f1f93475SJohn Baldwin if (error) 2211f1f93475SJohn Baldwin break; 2212fd7daa72SMichael Tuexen error = ktls_enable_rx(so, &tls); 2213f1f93475SJohn Baldwin break; 2214b2e60773SJohn Baldwin #endif 221508af8aacSRandall Stewart case TCP_MAXUNACKTIME: 22169077f387SGleb Smirnoff case TCP_KEEPIDLE: 22179077f387SGleb Smirnoff case TCP_KEEPINTVL: 22189077f387SGleb Smirnoff case TCP_KEEPINIT: 22199077f387SGleb Smirnoff INP_WUNLOCK(inp); 22209077f387SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 22219077f387SGleb Smirnoff if (error) 22229077f387SGleb Smirnoff return (error); 22239077f387SGleb Smirnoff 22249077f387SGleb Smirnoff if (ui > (UINT_MAX / hz)) { 22259077f387SGleb Smirnoff error = EINVAL; 22269077f387SGleb Smirnoff break; 22279077f387SGleb Smirnoff } 22289077f387SGleb Smirnoff ui *= hz; 22299077f387SGleb Smirnoff 22309077f387SGleb Smirnoff INP_WLOCK_RECHECK(inp); 22319077f387SGleb Smirnoff switch (sopt->sopt_name) { 223208af8aacSRandall Stewart case TCP_MAXUNACKTIME: 223308af8aacSRandall Stewart tp->t_maxunacktime = ui; 223408af8aacSRandall Stewart break; 223508af8aacSRandall Stewart 22369077f387SGleb Smirnoff case TCP_KEEPIDLE: 22379077f387SGleb Smirnoff tp->t_keepidle = ui; 22389077f387SGleb Smirnoff /* 22399077f387SGleb Smirnoff * XXX: better check current remaining 22409077f387SGleb Smirnoff * timeout and "merge" it with new value. 22419077f387SGleb Smirnoff */ 22429077f387SGleb Smirnoff if ((tp->t_state > TCPS_LISTEN) && 22439077f387SGleb Smirnoff (tp->t_state <= TCPS_CLOSING)) 22449077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 22459077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 22469077f387SGleb Smirnoff break; 22479077f387SGleb Smirnoff case TCP_KEEPINTVL: 22489077f387SGleb Smirnoff tp->t_keepintvl = ui; 22499077f387SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 22509077f387SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 22519077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 22529077f387SGleb Smirnoff TP_MAXIDLE(tp)); 22539077f387SGleb Smirnoff break; 22549077f387SGleb Smirnoff case TCP_KEEPINIT: 22559077f387SGleb Smirnoff tp->t_keepinit = ui; 22569077f387SGleb Smirnoff if (tp->t_state == TCPS_SYN_RECEIVED || 22579077f387SGleb Smirnoff tp->t_state == TCPS_SYN_SENT) 22589077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 22599077f387SGleb Smirnoff TP_KEEPINIT(tp)); 22609077f387SGleb Smirnoff break; 22619077f387SGleb Smirnoff } 226209fe6320SNavdeep Parhar goto unlock_and_done; 22639077f387SGleb Smirnoff 226485c05144SGleb Smirnoff case TCP_KEEPCNT: 226585c05144SGleb Smirnoff INP_WUNLOCK(inp); 226685c05144SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 226785c05144SGleb Smirnoff if (error) 226885c05144SGleb Smirnoff return (error); 226985c05144SGleb Smirnoff 227085c05144SGleb Smirnoff INP_WLOCK_RECHECK(inp); 227185c05144SGleb Smirnoff tp->t_keepcnt = ui; 227285c05144SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 227385c05144SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 227485c05144SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 227585c05144SGleb Smirnoff TP_MAXIDLE(tp)); 227685c05144SGleb Smirnoff goto unlock_and_done; 227785c05144SGleb Smirnoff 227886a996e6SHiren Panchasara #ifdef TCPPCAP 227986a996e6SHiren Panchasara case TCP_PCAP_OUT: 228086a996e6SHiren Panchasara case TCP_PCAP_IN: 228186a996e6SHiren Panchasara INP_WUNLOCK(inp); 228286a996e6SHiren Panchasara error = sooptcopyin(sopt, &optval, sizeof optval, 228386a996e6SHiren Panchasara sizeof optval); 228486a996e6SHiren Panchasara if (error) 228586a996e6SHiren Panchasara return (error); 228686a996e6SHiren Panchasara 228786a996e6SHiren Panchasara INP_WLOCK_RECHECK(inp); 228886a996e6SHiren Panchasara if (optval >= 0) 228986a996e6SHiren Panchasara tcp_pcap_set_sock_max(TCP_PCAP_OUT ? 229086a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts), 229186a996e6SHiren Panchasara optval); 229286a996e6SHiren Panchasara else 229386a996e6SHiren Panchasara error = EINVAL; 229486a996e6SHiren Panchasara goto unlock_and_done; 229586a996e6SHiren Panchasara #endif 229686a996e6SHiren Panchasara 2297c560df6fSPatrick Kelsey case TCP_FASTOPEN: { 2298c560df6fSPatrick Kelsey struct tcp_fastopen tfo_optval; 2299c560df6fSPatrick Kelsey 2300281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2301c560df6fSPatrick Kelsey if (!V_tcp_fastopen_client_enable && 2302c560df6fSPatrick Kelsey !V_tcp_fastopen_server_enable) 2303281a0fd4SPatrick Kelsey return (EPERM); 2304281a0fd4SPatrick Kelsey 2305c560df6fSPatrick Kelsey error = sooptcopyin(sopt, &tfo_optval, 2306c560df6fSPatrick Kelsey sizeof(tfo_optval), sizeof(int)); 2307281a0fd4SPatrick Kelsey if (error) 2308281a0fd4SPatrick Kelsey return (error); 2309281a0fd4SPatrick Kelsey 2310281a0fd4SPatrick Kelsey INP_WLOCK_RECHECK(inp); 2311d442a657SMichael Tuexen if ((tp->t_state != TCPS_CLOSED) && 2312d442a657SMichael Tuexen (tp->t_state != TCPS_LISTEN)) { 2313d442a657SMichael Tuexen error = EINVAL; 2314d442a657SMichael Tuexen goto unlock_and_done; 2315d442a657SMichael Tuexen } 2316c560df6fSPatrick Kelsey if (tfo_optval.enable) { 2317c560df6fSPatrick Kelsey if (tp->t_state == TCPS_LISTEN) { 2318c560df6fSPatrick Kelsey if (!V_tcp_fastopen_server_enable) { 2319c560df6fSPatrick Kelsey error = EPERM; 2320c560df6fSPatrick Kelsey goto unlock_and_done; 2321c560df6fSPatrick Kelsey } 2322c560df6fSPatrick Kelsey 2323c560df6fSPatrick Kelsey if (tp->t_tfo_pending == NULL) 2324281a0fd4SPatrick Kelsey tp->t_tfo_pending = 2325281a0fd4SPatrick Kelsey tcp_fastopen_alloc_counter(); 2326c560df6fSPatrick Kelsey } else { 2327c560df6fSPatrick Kelsey /* 2328c560df6fSPatrick Kelsey * If a pre-shared key was provided, 2329c560df6fSPatrick Kelsey * stash it in the client cookie 2330c560df6fSPatrick Kelsey * field of the tcpcb for use during 2331c560df6fSPatrick Kelsey * connect. 2332c560df6fSPatrick Kelsey */ 2333c560df6fSPatrick Kelsey if (sopt->sopt_valsize == 2334c560df6fSPatrick Kelsey sizeof(tfo_optval)) { 2335c560df6fSPatrick Kelsey memcpy(tp->t_tfo_cookie.client, 2336c560df6fSPatrick Kelsey tfo_optval.psk, 2337c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN); 2338c560df6fSPatrick Kelsey tp->t_tfo_client_cookie_len = 2339c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN; 2340c560df6fSPatrick Kelsey } 2341c560df6fSPatrick Kelsey } 2342d442a657SMichael Tuexen tp->t_flags |= TF_FASTOPEN; 2343281a0fd4SPatrick Kelsey } else 2344281a0fd4SPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 2345281a0fd4SPatrick Kelsey goto unlock_and_done; 2346c560df6fSPatrick Kelsey } 2347281a0fd4SPatrick Kelsey 2348e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 23492529f56eSJonathan T. Looney case TCP_LOG: 23502529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23512529f56eSJonathan T. Looney error = sooptcopyin(sopt, &optval, sizeof optval, 23522529f56eSJonathan T. Looney sizeof optval); 23532529f56eSJonathan T. Looney if (error) 23542529f56eSJonathan T. Looney return (error); 23552529f56eSJonathan T. Looney 23562529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 23572529f56eSJonathan T. Looney error = tcp_log_state_change(tp, optval); 23582529f56eSJonathan T. Looney goto unlock_and_done; 23592529f56eSJonathan T. Looney 23602529f56eSJonathan T. Looney case TCP_LOGBUF: 23612529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23622529f56eSJonathan T. Looney error = EINVAL; 23632529f56eSJonathan T. Looney break; 23642529f56eSJonathan T. Looney 23652529f56eSJonathan T. Looney case TCP_LOGID: 23662529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23672529f56eSJonathan T. Looney error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0); 23682529f56eSJonathan T. Looney if (error) 23692529f56eSJonathan T. Looney break; 23702529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 23712529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 23722529f56eSJonathan T. Looney error = tcp_log_set_id(tp, buf); 23732529f56eSJonathan T. Looney /* tcp_log_set_id() unlocks the INP. */ 23742529f56eSJonathan T. Looney break; 23752529f56eSJonathan T. Looney 23762529f56eSJonathan T. Looney case TCP_LOGDUMP: 23772529f56eSJonathan T. Looney case TCP_LOGDUMPID: 23782529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23792529f56eSJonathan T. Looney error = 23802529f56eSJonathan T. Looney sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0); 23812529f56eSJonathan T. Looney if (error) 23822529f56eSJonathan T. Looney break; 23832529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 23842529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 23852529f56eSJonathan T. Looney if (sopt->sopt_name == TCP_LOGDUMP) { 23862529f56eSJonathan T. Looney error = tcp_log_dump_tp_logbuf(tp, buf, 23872529f56eSJonathan T. Looney M_WAITOK, true); 23882529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23892529f56eSJonathan T. Looney } else { 23902529f56eSJonathan T. Looney tcp_log_dump_tp_bucket_logbufs(tp, buf); 23912529f56eSJonathan T. Looney /* 23922529f56eSJonathan T. Looney * tcp_log_dump_tp_bucket_logbufs() drops the 23932529f56eSJonathan T. Looney * INP lock. 23942529f56eSJonathan T. Looney */ 23952529f56eSJonathan T. Looney } 23962529f56eSJonathan T. Looney break; 2397e24e5683SJonathan T. Looney #endif 23982529f56eSJonathan T. Looney 2399df8bae1dSRodney W. Grimes default: 24008501a69cSRobert Watson INP_WUNLOCK(inp); 2401df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2402df8bae1dSRodney W. Grimes break; 2403df8bae1dSRodney W. Grimes } 2404df8bae1dSRodney W. Grimes break; 2405df8bae1dSRodney W. Grimes 2406cfe8b629SGarrett Wollman case SOPT_GET: 24071e8f5ffaSRobert Watson tp = intotcpcb(inp); 2408cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2409fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 241088f6b043SBruce M Simpson case TCP_MD5SIG: 24118501a69cSRobert Watson INP_WUNLOCK(inp); 241297453e5eSClaudio Jeker if (!TCPMD5_ENABLED()) 2413fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 2414fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 24151cfd4b53SBruce M Simpson break; 2416265ed012SBruce M Simpson #endif 24171e8f5ffaSRobert Watson 2418df8bae1dSRodney W. Grimes case TCP_NODELAY: 2419cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 24208501a69cSRobert Watson INP_WUNLOCK(inp); 2421b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2422df8bae1dSRodney W. Grimes break; 2423df8bae1dSRodney W. Grimes case TCP_MAXSEG: 2424cfe8b629SGarrett Wollman optval = tp->t_maxseg; 24258501a69cSRobert Watson INP_WUNLOCK(inp); 2426b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2427df8bae1dSRodney W. Grimes break; 24289e644c23SMichael Tuexen case TCP_REMOTE_UDP_ENCAPS_PORT: 24299e644c23SMichael Tuexen optval = ntohs(tp->t_port); 24309e644c23SMichael Tuexen INP_WUNLOCK(inp); 24319e644c23SMichael Tuexen error = sooptcopyout(sopt, &optval, sizeof optval); 24329e644c23SMichael Tuexen break; 2433a0292f23SGarrett Wollman case TCP_NOOPT: 2434cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 24358501a69cSRobert Watson INP_WUNLOCK(inp); 2436b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2437a0292f23SGarrett Wollman break; 2438a0292f23SGarrett Wollman case TCP_NOPUSH: 2439cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 24408501a69cSRobert Watson INP_WUNLOCK(inp); 2441b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2442b8af5dfaSRobert Watson break; 2443b8af5dfaSRobert Watson case TCP_INFO: 2444b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 24458501a69cSRobert Watson INP_WUNLOCK(inp); 2446b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 2447a0292f23SGarrett Wollman break; 2448adc56f5aSEdward Tomasz Napierala case TCP_STATS: 2449adc56f5aSEdward Tomasz Napierala { 2450adc56f5aSEdward Tomasz Napierala #ifdef STATS 2451adc56f5aSEdward Tomasz Napierala int nheld; 2452adc56f5aSEdward Tomasz Napierala TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0; 2453adc56f5aSEdward Tomasz Napierala 2454adc56f5aSEdward Tomasz Napierala error = 0; 2455adc56f5aSEdward Tomasz Napierala socklen_t outsbsz = sopt->sopt_valsize; 2456adc56f5aSEdward Tomasz Napierala if (tp->t_stats == NULL) 2457adc56f5aSEdward Tomasz Napierala error = ENOENT; 2458adc56f5aSEdward Tomasz Napierala else if (outsbsz >= tp->t_stats->cursz) 2459adc56f5aSEdward Tomasz Napierala outsbsz = tp->t_stats->cursz; 2460adc56f5aSEdward Tomasz Napierala else if (outsbsz >= sizeof(struct statsblob)) 2461adc56f5aSEdward Tomasz Napierala outsbsz = sizeof(struct statsblob); 2462adc56f5aSEdward Tomasz Napierala else 2463adc56f5aSEdward Tomasz Napierala error = EINVAL; 2464adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2465adc56f5aSEdward Tomasz Napierala if (error) 2466adc56f5aSEdward Tomasz Napierala break; 2467adc56f5aSEdward Tomasz Napierala 2468adc56f5aSEdward Tomasz Napierala sbp = sopt->sopt_val; 2469adc56f5aSEdward Tomasz Napierala nheld = atop(round_page(((vm_offset_t)sbp) + 2470adc56f5aSEdward Tomasz Napierala (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp)); 2471adc56f5aSEdward Tomasz Napierala vm_page_t ma[nheld]; 2472adc56f5aSEdward Tomasz Napierala if (vm_fault_quick_hold_pages( 2473adc56f5aSEdward Tomasz Napierala &curproc->p_vmspace->vm_map, (vm_offset_t)sbp, 2474adc56f5aSEdward Tomasz Napierala outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma, 2475adc56f5aSEdward Tomasz Napierala nheld) < 0) { 2476adc56f5aSEdward Tomasz Napierala error = EFAULT; 2477adc56f5aSEdward Tomasz Napierala break; 2478adc56f5aSEdward Tomasz Napierala } 2479adc56f5aSEdward Tomasz Napierala 2480adc56f5aSEdward Tomasz Napierala if ((error = copyin_nofault(&(sbp->flags), &sbflags, 2481adc56f5aSEdward Tomasz Napierala SIZEOF_MEMBER(struct statsblob, flags)))) 2482adc56f5aSEdward Tomasz Napierala goto unhold; 2483adc56f5aSEdward Tomasz Napierala 2484adc56f5aSEdward Tomasz Napierala INP_WLOCK_RECHECK(inp); 2485adc56f5aSEdward Tomasz Napierala error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats, 2486adc56f5aSEdward Tomasz Napierala sbflags | SB_CLONE_USRDSTNOFAULT); 2487adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2488adc56f5aSEdward Tomasz Napierala sopt->sopt_valsize = outsbsz; 2489adc56f5aSEdward Tomasz Napierala unhold: 2490adc56f5aSEdward Tomasz Napierala vm_page_unhold_pages(ma, nheld); 2491adc56f5aSEdward Tomasz Napierala #else 2492adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2493adc56f5aSEdward Tomasz Napierala error = EOPNOTSUPP; 2494adc56f5aSEdward Tomasz Napierala #endif /* !STATS */ 2495adc56f5aSEdward Tomasz Napierala break; 2496adc56f5aSEdward Tomasz Napierala } 2497dbc42409SLawrence Stewart case TCP_CONGESTION: 2498af6fef3aSGleb Smirnoff len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 2499dbc42409SLawrence Stewart INP_WUNLOCK(inp); 2500af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, buf, len + 1); 2501dbc42409SLawrence Stewart break; 250208af8aacSRandall Stewart case TCP_MAXUNACKTIME: 25032f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 25042f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 25052f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 25062f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 25072f3eb7f4SGleb Smirnoff switch (sopt->sopt_name) { 250808af8aacSRandall Stewart case TCP_MAXUNACKTIME: 250908af8aacSRandall Stewart ui = TP_MAXUNACKTIME(tp) / hz; 251008af8aacSRandall Stewart break; 25112f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 25125a17b6adSMichael Tuexen ui = TP_KEEPIDLE(tp) / hz; 25132f3eb7f4SGleb Smirnoff break; 25142f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 25155a17b6adSMichael Tuexen ui = TP_KEEPINTVL(tp) / hz; 25162f3eb7f4SGleb Smirnoff break; 25172f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 25185a17b6adSMichael Tuexen ui = TP_KEEPINIT(tp) / hz; 25192f3eb7f4SGleb Smirnoff break; 25202f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 25215a17b6adSMichael Tuexen ui = TP_KEEPCNT(tp); 25222f3eb7f4SGleb Smirnoff break; 25232f3eb7f4SGleb Smirnoff } 25242f3eb7f4SGleb Smirnoff INP_WUNLOCK(inp); 25252f3eb7f4SGleb Smirnoff error = sooptcopyout(sopt, &ui, sizeof(ui)); 25262f3eb7f4SGleb Smirnoff break; 252786a996e6SHiren Panchasara #ifdef TCPPCAP 252886a996e6SHiren Panchasara case TCP_PCAP_OUT: 252986a996e6SHiren Panchasara case TCP_PCAP_IN: 253086a996e6SHiren Panchasara optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ? 253186a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts)); 253286a996e6SHiren Panchasara INP_WUNLOCK(inp); 253386a996e6SHiren Panchasara error = sooptcopyout(sopt, &optval, sizeof optval); 253486a996e6SHiren Panchasara break; 253586a996e6SHiren Panchasara #endif 2536281a0fd4SPatrick Kelsey case TCP_FASTOPEN: 2537281a0fd4SPatrick Kelsey optval = tp->t_flags & TF_FASTOPEN; 2538281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2539281a0fd4SPatrick Kelsey error = sooptcopyout(sopt, &optval, sizeof optval); 2540281a0fd4SPatrick Kelsey break; 2541e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 25422529f56eSJonathan T. Looney case TCP_LOG: 25432529f56eSJonathan T. Looney optval = tp->t_logstate; 25442529f56eSJonathan T. Looney INP_WUNLOCK(inp); 25452529f56eSJonathan T. Looney error = sooptcopyout(sopt, &optval, sizeof(optval)); 25462529f56eSJonathan T. Looney break; 25472529f56eSJonathan T. Looney case TCP_LOGBUF: 25482529f56eSJonathan T. Looney /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */ 25492529f56eSJonathan T. Looney error = tcp_log_getlogbuf(sopt, tp); 25502529f56eSJonathan T. Looney break; 25512529f56eSJonathan T. Looney case TCP_LOGID: 25522529f56eSJonathan T. Looney len = tcp_log_get_id(tp, buf); 25532529f56eSJonathan T. Looney INP_WUNLOCK(inp); 25542529f56eSJonathan T. Looney error = sooptcopyout(sopt, buf, len + 1); 25552529f56eSJonathan T. Looney break; 25562529f56eSJonathan T. Looney case TCP_LOGDUMP: 25572529f56eSJonathan T. Looney case TCP_LOGDUMPID: 25582529f56eSJonathan T. Looney INP_WUNLOCK(inp); 25592529f56eSJonathan T. Looney error = EINVAL; 25602529f56eSJonathan T. Looney break; 2561e24e5683SJonathan T. Looney #endif 2562b2e60773SJohn Baldwin #ifdef KERN_TLS 2563b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2564fd7daa72SMichael Tuexen error = ktls_get_tx_mode(so, &optval); 2565b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2566bf256782SMark Johnston if (error == 0) 2567bf256782SMark Johnston error = sooptcopyout(sopt, &optval, 2568bf256782SMark Johnston sizeof(optval)); 2569b2e60773SJohn Baldwin break; 2570f1f93475SJohn Baldwin case TCP_RXTLS_MODE: 2571fd7daa72SMichael Tuexen error = ktls_get_rx_mode(so, &optval); 2572f1f93475SJohn Baldwin INP_WUNLOCK(inp); 2573bf256782SMark Johnston if (error == 0) 2574bf256782SMark Johnston error = sooptcopyout(sopt, &optval, 2575bf256782SMark Johnston sizeof(optval)); 2576f1f93475SJohn Baldwin break; 2577b2e60773SJohn Baldwin #endif 25780471a8c7SRichard Scheffenegger case TCP_LRD: 25790471a8c7SRichard Scheffenegger optval = tp->t_flags & TF_LRD; 25800471a8c7SRichard Scheffenegger INP_WUNLOCK(inp); 25810471a8c7SRichard Scheffenegger error = sooptcopyout(sopt, &optval, sizeof optval); 25820471a8c7SRichard Scheffenegger break; 2583df8bae1dSRodney W. Grimes default: 25848501a69cSRobert Watson INP_WUNLOCK(inp); 2585df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2586df8bae1dSRodney W. Grimes break; 2587df8bae1dSRodney W. Grimes } 2588df8bae1dSRodney W. Grimes break; 2589df8bae1dSRodney W. Grimes } 2590df8bae1dSRodney W. Grimes return (error); 2591df8bae1dSRodney W. Grimes } 25928501a69cSRobert Watson #undef INP_WLOCK_RECHECK 2593bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP 2594df8bae1dSRodney W. Grimes 259526e30fbbSDavid Greenman /* 2596df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 2597df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 2598df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 2599df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 2600df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 2601df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 2602df8bae1dSRodney W. Grimes */ 2603623dce13SRobert Watson static void 2604ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp) 2605df8bae1dSRodney W. Grimes { 26069eb0e832SGleb Smirnoff struct inpcb *inp = tptoinpcb(tp); 26079eb0e832SGleb Smirnoff struct socket *so = tptosocket(tp); 2608e6e0b5ffSRobert Watson 260997a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 26108501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2611df8bae1dSRodney W. Grimes 2612623dce13SRobert Watson /* 2613623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 2614623dce13SRobert Watson * socket is still open. 2615623dce13SRobert Watson */ 26168db239dcSMichael Tuexen if (tp->t_state < TCPS_ESTABLISHED && 26178db239dcSMichael Tuexen !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) { 2618df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2619623dce13SRobert Watson KASSERT(tp != NULL, 2620623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 2621623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 2622243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 2623623dce13SRobert Watson KASSERT(tp != NULL, 2624623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 2625623dce13SRobert Watson } else { 2626df8bae1dSRodney W. Grimes soisdisconnecting(so); 2627df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 2628623dce13SRobert Watson tcp_usrclosed(tp); 2629ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 2630f64dc2abSGleb Smirnoff /* Ignore stack's drop request, we already at it. */ 2631f64dc2abSGleb Smirnoff (void)tcp_output_nodrop(tp); 2632df8bae1dSRodney W. Grimes } 2633df8bae1dSRodney W. Grimes } 2634df8bae1dSRodney W. Grimes 2635df8bae1dSRodney W. Grimes /* 2636df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 2637df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 2638df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 2639df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 2640df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 2641df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 2642df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 2643df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 2644df8bae1dSRodney W. Grimes */ 2645623dce13SRobert Watson static void 2646ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp) 2647df8bae1dSRodney W. Grimes { 2648df8bae1dSRodney W. Grimes 264997a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 26509eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 2651e6e0b5ffSRobert Watson 2652df8bae1dSRodney W. Grimes switch (tp->t_state) { 2653df8bae1dSRodney W. Grimes case TCPS_LISTEN: 265409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 265509fe6320SNavdeep Parhar tcp_offload_listen_stop(tp); 265609fe6320SNavdeep Parhar #endif 2657550e9d42SHiren Panchasara tcp_state_change(tp, TCPS_CLOSED); 2658bc65987aSKip Macy /* FALLTHROUGH */ 2659bc65987aSKip Macy case TCPS_CLOSED: 2660df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2661623dce13SRobert Watson /* 2662623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 2663623dce13SRobert Watson * still open. 2664623dce13SRobert Watson */ 2665623dce13SRobert Watson KASSERT(tp != NULL, 2666623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 2667df8bae1dSRodney W. Grimes break; 2668df8bae1dSRodney W. Grimes 2669a0292f23SGarrett Wollman case TCPS_SYN_SENT: 2670df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2671a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 2672a0292f23SGarrett Wollman break; 2673a0292f23SGarrett Wollman 2674df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 267557f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2676df8bae1dSRodney W. Grimes break; 2677df8bae1dSRodney W. Grimes 2678df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 267957f60867SMark Johnston tcp_state_change(tp, TCPS_LAST_ACK); 2680df8bae1dSRodney W. Grimes break; 2681df8bae1dSRodney W. Grimes } 268208af8aacSRandall Stewart if (tp->t_acktime == 0) 268308af8aacSRandall Stewart tp->t_acktime = ticks; 2684abc7d910SRobert Watson if (tp->t_state >= TCPS_FIN_WAIT_2) { 26859eb0e832SGleb Smirnoff soisdisconnected(tptosocket(tp)); 2686abc7d910SRobert Watson /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 26877c72af87SMohan Srinivasan if (tp->t_state == TCPS_FIN_WAIT_2) { 26887c72af87SMohan Srinivasan int timeout; 26897c72af87SMohan Srinivasan 26907c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 26919077f387SGleb Smirnoff tcp_finwait2_timeout : TP_MAXIDLE(tp); 2692b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 2693b6239c4aSAndras Olah } 2694df8bae1dSRodney W. Grimes } 26957c72af87SMohan Srinivasan } 2696497057eeSRobert Watson 2697497057eeSRobert Watson #ifdef DDB 2698497057eeSRobert Watson static void 2699497057eeSRobert Watson db_print_indent(int indent) 2700497057eeSRobert Watson { 2701497057eeSRobert Watson int i; 2702497057eeSRobert Watson 2703497057eeSRobert Watson for (i = 0; i < indent; i++) 2704497057eeSRobert Watson db_printf(" "); 2705497057eeSRobert Watson } 2706497057eeSRobert Watson 2707497057eeSRobert Watson static void 2708497057eeSRobert Watson db_print_tstate(int t_state) 2709497057eeSRobert Watson { 2710497057eeSRobert Watson 2711497057eeSRobert Watson switch (t_state) { 2712497057eeSRobert Watson case TCPS_CLOSED: 2713497057eeSRobert Watson db_printf("TCPS_CLOSED"); 2714497057eeSRobert Watson return; 2715497057eeSRobert Watson 2716497057eeSRobert Watson case TCPS_LISTEN: 2717497057eeSRobert Watson db_printf("TCPS_LISTEN"); 2718497057eeSRobert Watson return; 2719497057eeSRobert Watson 2720497057eeSRobert Watson case TCPS_SYN_SENT: 2721497057eeSRobert Watson db_printf("TCPS_SYN_SENT"); 2722497057eeSRobert Watson return; 2723497057eeSRobert Watson 2724497057eeSRobert Watson case TCPS_SYN_RECEIVED: 2725497057eeSRobert Watson db_printf("TCPS_SYN_RECEIVED"); 2726497057eeSRobert Watson return; 2727497057eeSRobert Watson 2728497057eeSRobert Watson case TCPS_ESTABLISHED: 2729497057eeSRobert Watson db_printf("TCPS_ESTABLISHED"); 2730497057eeSRobert Watson return; 2731497057eeSRobert Watson 2732497057eeSRobert Watson case TCPS_CLOSE_WAIT: 2733497057eeSRobert Watson db_printf("TCPS_CLOSE_WAIT"); 2734497057eeSRobert Watson return; 2735497057eeSRobert Watson 2736497057eeSRobert Watson case TCPS_FIN_WAIT_1: 2737497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_1"); 2738497057eeSRobert Watson return; 2739497057eeSRobert Watson 2740497057eeSRobert Watson case TCPS_CLOSING: 2741497057eeSRobert Watson db_printf("TCPS_CLOSING"); 2742497057eeSRobert Watson return; 2743497057eeSRobert Watson 2744497057eeSRobert Watson case TCPS_LAST_ACK: 2745497057eeSRobert Watson db_printf("TCPS_LAST_ACK"); 2746497057eeSRobert Watson return; 2747497057eeSRobert Watson 2748497057eeSRobert Watson case TCPS_FIN_WAIT_2: 2749497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_2"); 2750497057eeSRobert Watson return; 2751497057eeSRobert Watson 2752497057eeSRobert Watson case TCPS_TIME_WAIT: 2753497057eeSRobert Watson db_printf("TCPS_TIME_WAIT"); 2754497057eeSRobert Watson return; 2755497057eeSRobert Watson 2756497057eeSRobert Watson default: 2757497057eeSRobert Watson db_printf("unknown"); 2758497057eeSRobert Watson return; 2759497057eeSRobert Watson } 2760497057eeSRobert Watson } 2761497057eeSRobert Watson 2762497057eeSRobert Watson static void 2763497057eeSRobert Watson db_print_tflags(u_int t_flags) 2764497057eeSRobert Watson { 2765497057eeSRobert Watson int comma; 2766497057eeSRobert Watson 2767497057eeSRobert Watson comma = 0; 2768497057eeSRobert Watson if (t_flags & TF_ACKNOW) { 2769497057eeSRobert Watson db_printf("%sTF_ACKNOW", comma ? ", " : ""); 2770497057eeSRobert Watson comma = 1; 2771497057eeSRobert Watson } 2772497057eeSRobert Watson if (t_flags & TF_DELACK) { 2773497057eeSRobert Watson db_printf("%sTF_DELACK", comma ? ", " : ""); 2774497057eeSRobert Watson comma = 1; 2775497057eeSRobert Watson } 2776497057eeSRobert Watson if (t_flags & TF_NODELAY) { 2777497057eeSRobert Watson db_printf("%sTF_NODELAY", comma ? ", " : ""); 2778497057eeSRobert Watson comma = 1; 2779497057eeSRobert Watson } 2780497057eeSRobert Watson if (t_flags & TF_NOOPT) { 2781497057eeSRobert Watson db_printf("%sTF_NOOPT", comma ? ", " : ""); 2782497057eeSRobert Watson comma = 1; 2783497057eeSRobert Watson } 2784497057eeSRobert Watson if (t_flags & TF_SENTFIN) { 2785497057eeSRobert Watson db_printf("%sTF_SENTFIN", comma ? ", " : ""); 2786497057eeSRobert Watson comma = 1; 2787497057eeSRobert Watson } 2788497057eeSRobert Watson if (t_flags & TF_REQ_SCALE) { 2789497057eeSRobert Watson db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 2790497057eeSRobert Watson comma = 1; 2791497057eeSRobert Watson } 2792497057eeSRobert Watson if (t_flags & TF_RCVD_SCALE) { 2793497057eeSRobert Watson db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 2794497057eeSRobert Watson comma = 1; 2795497057eeSRobert Watson } 2796497057eeSRobert Watson if (t_flags & TF_REQ_TSTMP) { 2797497057eeSRobert Watson db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 2798497057eeSRobert Watson comma = 1; 2799497057eeSRobert Watson } 2800497057eeSRobert Watson if (t_flags & TF_RCVD_TSTMP) { 2801497057eeSRobert Watson db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 2802497057eeSRobert Watson comma = 1; 2803497057eeSRobert Watson } 2804497057eeSRobert Watson if (t_flags & TF_SACK_PERMIT) { 2805497057eeSRobert Watson db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 2806497057eeSRobert Watson comma = 1; 2807497057eeSRobert Watson } 2808497057eeSRobert Watson if (t_flags & TF_NEEDSYN) { 2809497057eeSRobert Watson db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 2810497057eeSRobert Watson comma = 1; 2811497057eeSRobert Watson } 2812497057eeSRobert Watson if (t_flags & TF_NEEDFIN) { 2813497057eeSRobert Watson db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 2814497057eeSRobert Watson comma = 1; 2815497057eeSRobert Watson } 2816497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 2817497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 2818497057eeSRobert Watson comma = 1; 2819497057eeSRobert Watson } 28203f169c54SRichard Scheffenegger if (t_flags & TF_PREVVALID) { 28213f169c54SRichard Scheffenegger db_printf("%sTF_PREVVALID", comma ? ", " : ""); 28223f169c54SRichard Scheffenegger comma = 1; 28233f169c54SRichard Scheffenegger } 2824497057eeSRobert Watson if (t_flags & TF_MORETOCOME) { 2825497057eeSRobert Watson db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 2826497057eeSRobert Watson comma = 1; 2827497057eeSRobert Watson } 2828493105c2SGleb Smirnoff if (t_flags & TF_SONOTCONN) { 2829493105c2SGleb Smirnoff db_printf("%sTF_SONOTCONN", comma ? ", " : ""); 2830497057eeSRobert Watson comma = 1; 2831497057eeSRobert Watson } 2832497057eeSRobert Watson if (t_flags & TF_LASTIDLE) { 2833497057eeSRobert Watson db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 2834497057eeSRobert Watson comma = 1; 2835497057eeSRobert Watson } 2836497057eeSRobert Watson if (t_flags & TF_RXWIN0SENT) { 2837497057eeSRobert Watson db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 2838497057eeSRobert Watson comma = 1; 2839497057eeSRobert Watson } 2840497057eeSRobert Watson if (t_flags & TF_FASTRECOVERY) { 2841497057eeSRobert Watson db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 2842497057eeSRobert Watson comma = 1; 2843497057eeSRobert Watson } 2844dbc42409SLawrence Stewart if (t_flags & TF_CONGRECOVERY) { 2845dbc42409SLawrence Stewart db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 2846dbc42409SLawrence Stewart comma = 1; 2847dbc42409SLawrence Stewart } 2848497057eeSRobert Watson if (t_flags & TF_WASFRECOVERY) { 2849497057eeSRobert Watson db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 2850497057eeSRobert Watson comma = 1; 2851497057eeSRobert Watson } 28523f169c54SRichard Scheffenegger if (t_flags & TF_WASCRECOVERY) { 28533f169c54SRichard Scheffenegger db_printf("%sTF_WASCRECOVERY", comma ? ", " : ""); 28543f169c54SRichard Scheffenegger comma = 1; 28553f169c54SRichard Scheffenegger } 2856497057eeSRobert Watson if (t_flags & TF_SIGNATURE) { 2857497057eeSRobert Watson db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 2858497057eeSRobert Watson comma = 1; 2859497057eeSRobert Watson } 2860497057eeSRobert Watson if (t_flags & TF_FORCEDATA) { 2861497057eeSRobert Watson db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 2862497057eeSRobert Watson comma = 1; 2863497057eeSRobert Watson } 2864497057eeSRobert Watson if (t_flags & TF_TSO) { 2865497057eeSRobert Watson db_printf("%sTF_TSO", comma ? ", " : ""); 2866497057eeSRobert Watson comma = 1; 2867497057eeSRobert Watson } 2868281a0fd4SPatrick Kelsey if (t_flags & TF_FASTOPEN) { 2869281a0fd4SPatrick Kelsey db_printf("%sTF_FASTOPEN", comma ? ", " : ""); 2870281a0fd4SPatrick Kelsey comma = 1; 2871281a0fd4SPatrick Kelsey } 2872497057eeSRobert Watson } 2873497057eeSRobert Watson 2874497057eeSRobert Watson static void 28753cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2) 28763cf38784SMichael Tuexen { 28773cf38784SMichael Tuexen int comma; 28783cf38784SMichael Tuexen 28793cf38784SMichael Tuexen comma = 0; 28803f169c54SRichard Scheffenegger if (t_flags2 & TF2_PLPMTU_BLACKHOLE) { 28813f169c54SRichard Scheffenegger db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : ""); 28823f169c54SRichard Scheffenegger comma = 1; 28833f169c54SRichard Scheffenegger } 28843f169c54SRichard Scheffenegger if (t_flags2 & TF2_PLPMTU_PMTUD) { 28853f169c54SRichard Scheffenegger db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : ""); 28863f169c54SRichard Scheffenegger comma = 1; 28873f169c54SRichard Scheffenegger } 28883f169c54SRichard Scheffenegger if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) { 28893f169c54SRichard Scheffenegger db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : ""); 28903f169c54SRichard Scheffenegger comma = 1; 28913f169c54SRichard Scheffenegger } 28923f169c54SRichard Scheffenegger if (t_flags2 & TF2_LOG_AUTO) { 28933f169c54SRichard Scheffenegger db_printf("%sTF2_LOG_AUTO", comma ? ", " : ""); 28943f169c54SRichard Scheffenegger comma = 1; 28953f169c54SRichard Scheffenegger } 28963f169c54SRichard Scheffenegger if (t_flags2 & TF2_DROP_AF_DATA) { 28973f169c54SRichard Scheffenegger db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : ""); 28983f169c54SRichard Scheffenegger comma = 1; 28993f169c54SRichard Scheffenegger } 29003cf38784SMichael Tuexen if (t_flags2 & TF2_ECN_PERMIT) { 29013cf38784SMichael Tuexen db_printf("%sTF2_ECN_PERMIT", comma ? ", " : ""); 29023cf38784SMichael Tuexen comma = 1; 29033cf38784SMichael Tuexen } 29043f169c54SRichard Scheffenegger if (t_flags2 & TF2_ECN_SND_CWR) { 29053f169c54SRichard Scheffenegger db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : ""); 29063f169c54SRichard Scheffenegger comma = 1; 29073f169c54SRichard Scheffenegger } 29083f169c54SRichard Scheffenegger if (t_flags2 & TF2_ECN_SND_ECE) { 29093f169c54SRichard Scheffenegger db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : ""); 29103f169c54SRichard Scheffenegger comma = 1; 29113f169c54SRichard Scheffenegger } 29123f169c54SRichard Scheffenegger if (t_flags2 & TF2_ACE_PERMIT) { 29133f169c54SRichard Scheffenegger db_printf("%sTF2_ACE_PERMIT", comma ? ", " : ""); 29143f169c54SRichard Scheffenegger comma = 1; 29153f169c54SRichard Scheffenegger } 29163f169c54SRichard Scheffenegger if (t_flags2 & TF2_FBYTES_COMPLETE) { 29173f169c54SRichard Scheffenegger db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : ""); 29183f169c54SRichard Scheffenegger comma = 1; 29193f169c54SRichard Scheffenegger } 29203cf38784SMichael Tuexen } 29213cf38784SMichael Tuexen 29223cf38784SMichael Tuexen static void 2923497057eeSRobert Watson db_print_toobflags(char t_oobflags) 2924497057eeSRobert Watson { 2925497057eeSRobert Watson int comma; 2926497057eeSRobert Watson 2927497057eeSRobert Watson comma = 0; 2928497057eeSRobert Watson if (t_oobflags & TCPOOB_HAVEDATA) { 2929497057eeSRobert Watson db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 2930497057eeSRobert Watson comma = 1; 2931497057eeSRobert Watson } 2932497057eeSRobert Watson if (t_oobflags & TCPOOB_HADDATA) { 2933497057eeSRobert Watson db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 2934497057eeSRobert Watson comma = 1; 2935497057eeSRobert Watson } 2936497057eeSRobert Watson } 2937497057eeSRobert Watson 2938497057eeSRobert Watson static void 2939497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 2940497057eeSRobert Watson { 2941497057eeSRobert Watson 2942497057eeSRobert Watson db_print_indent(indent); 2943497057eeSRobert Watson db_printf("%s at %p\n", name, tp); 2944497057eeSRobert Watson 2945497057eeSRobert Watson indent += 2; 2946497057eeSRobert Watson 2947497057eeSRobert Watson db_print_indent(indent); 2948497057eeSRobert Watson db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 2949c28440dbSRandall Stewart TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 2950497057eeSRobert Watson 2951497057eeSRobert Watson db_print_indent(indent); 2952446ccdd0SGleb Smirnoff db_printf("t_callout: %p t_timers: %p\n", 2953446ccdd0SGleb Smirnoff &tp->t_callout, &tp->t_timers); 2954497057eeSRobert Watson 2955497057eeSRobert Watson db_print_indent(indent); 2956497057eeSRobert Watson db_printf("t_state: %d (", tp->t_state); 2957497057eeSRobert Watson db_print_tstate(tp->t_state); 2958497057eeSRobert Watson db_printf(")\n"); 2959497057eeSRobert Watson 2960497057eeSRobert Watson db_print_indent(indent); 2961497057eeSRobert Watson db_printf("t_flags: 0x%x (", tp->t_flags); 2962497057eeSRobert Watson db_print_tflags(tp->t_flags); 2963497057eeSRobert Watson db_printf(")\n"); 2964497057eeSRobert Watson 2965497057eeSRobert Watson db_print_indent(indent); 29663cf38784SMichael Tuexen db_printf("t_flags2: 0x%x (", tp->t_flags2); 29673cf38784SMichael Tuexen db_print_tflags2(tp->t_flags2); 29683cf38784SMichael Tuexen db_printf(")\n"); 29693cf38784SMichael Tuexen 29703cf38784SMichael Tuexen db_print_indent(indent); 2971fb8f221aSMaxim Konovalov db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: 0x%08x\n", 2972497057eeSRobert Watson tp->snd_una, tp->snd_max, tp->snd_nxt); 2973497057eeSRobert Watson 2974497057eeSRobert Watson db_print_indent(indent); 2975497057eeSRobert Watson db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 2976497057eeSRobert Watson tp->snd_up, tp->snd_wl1, tp->snd_wl2); 2977497057eeSRobert Watson 2978497057eeSRobert Watson db_print_indent(indent); 2979497057eeSRobert Watson db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 2980497057eeSRobert Watson tp->iss, tp->irs, tp->rcv_nxt); 2981497057eeSRobert Watson 2982497057eeSRobert Watson db_print_indent(indent); 29833ac12506SJonathan T. Looney db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n", 2984497057eeSRobert Watson tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 2985497057eeSRobert Watson 2986497057eeSRobert Watson db_print_indent(indent); 29873ac12506SJonathan T. Looney db_printf("snd_wnd: %u snd_cwnd: %u\n", 29881c18314dSAndre Oppermann tp->snd_wnd, tp->snd_cwnd); 2989497057eeSRobert Watson 2990497057eeSRobert Watson db_print_indent(indent); 29913ac12506SJonathan T. Looney db_printf("snd_ssthresh: %u snd_recover: " 29921c18314dSAndre Oppermann "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 2993497057eeSRobert Watson 2994497057eeSRobert Watson db_print_indent(indent); 29950c39d38dSGleb Smirnoff db_printf("t_rcvtime: %u t_startime: %u\n", 29960c39d38dSGleb Smirnoff tp->t_rcvtime, tp->t_starttime); 2997497057eeSRobert Watson 2998497057eeSRobert Watson db_print_indent(indent); 29991c18314dSAndre Oppermann db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 30001c18314dSAndre Oppermann tp->t_rtttime, tp->t_rtseq); 3001497057eeSRobert Watson 3002497057eeSRobert Watson db_print_indent(indent); 30031c18314dSAndre Oppermann db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 30041c18314dSAndre Oppermann tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 3005497057eeSRobert Watson 3006497057eeSRobert Watson db_print_indent(indent); 3007bd4f9866SMichael Tuexen db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u\n", 3008bd4f9866SMichael Tuexen tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin); 3009497057eeSRobert Watson 3010497057eeSRobert Watson db_print_indent(indent); 301118b83b62SRichard Scheffenegger db_printf("t_rttupdated: %u max_sndwnd: %u t_softerror: %d\n", 3012497057eeSRobert Watson tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 3013497057eeSRobert Watson 3014497057eeSRobert Watson db_print_indent(indent); 3015497057eeSRobert Watson db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 3016497057eeSRobert Watson db_print_toobflags(tp->t_oobflags); 3017497057eeSRobert Watson db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 3018497057eeSRobert Watson 3019497057eeSRobert Watson db_print_indent(indent); 3020497057eeSRobert Watson db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 3021497057eeSRobert Watson tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 3022497057eeSRobert Watson 3023497057eeSRobert Watson db_print_indent(indent); 30249f78a87aSJohn Baldwin db_printf("ts_recent: %u ts_recent_age: %u\n", 30251a553740SAndre Oppermann tp->ts_recent, tp->ts_recent_age); 3026497057eeSRobert Watson 3027497057eeSRobert Watson db_print_indent(indent); 3028497057eeSRobert Watson db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 30293ac12506SJonathan T. Looney "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 3030497057eeSRobert Watson 3031497057eeSRobert Watson db_print_indent(indent); 30323ac12506SJonathan T. Looney db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x " 30339f78a87aSJohn Baldwin "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 3034497057eeSRobert Watson tp->snd_recover_prev, tp->t_badrxtwin); 3035497057eeSRobert Watson 3036497057eeSRobert Watson db_print_indent(indent); 30373529149eSAndre Oppermann db_printf("snd_numholes: %d snd_holes first: %p\n", 30383529149eSAndre Oppermann tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 3039497057eeSRobert Watson 3040497057eeSRobert Watson db_print_indent(indent); 3041a3574665SMichael Tuexen db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n", 3042a3574665SMichael Tuexen tp->snd_fack, tp->rcv_numsacks); 3043497057eeSRobert Watson 3044497057eeSRobert Watson /* Skip sackblks, sackhint. */ 3045497057eeSRobert Watson 3046497057eeSRobert Watson db_print_indent(indent); 3047497057eeSRobert Watson db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 3048497057eeSRobert Watson tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 3049497057eeSRobert Watson } 3050497057eeSRobert Watson 3051497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 3052497057eeSRobert Watson { 3053497057eeSRobert Watson struct tcpcb *tp; 3054497057eeSRobert Watson 3055497057eeSRobert Watson if (!have_addr) { 3056497057eeSRobert Watson db_printf("usage: show tcpcb <addr>\n"); 3057497057eeSRobert Watson return; 3058497057eeSRobert Watson } 3059497057eeSRobert Watson tp = (struct tcpcb *)addr; 3060497057eeSRobert Watson 3061497057eeSRobert Watson db_print_tcpcb(tp, "tcpcb", 0); 3062497057eeSRobert Watson } 3063497057eeSRobert Watson #endif 3064