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 1214d77a549SAlfred Perlstein static int tcp_connect(struct tcpcb *, struct sockaddr *, 1224d77a549SAlfred Perlstein struct thread *td); 123b287c6c7SBjoern A. Zeeb #endif /* INET */ 124fb59c426SYoshinobu Inoue #ifdef INET6 1254d77a549SAlfred Perlstein static int tcp6_connect(struct tcpcb *, struct sockaddr *, 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); 485b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, 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); 577b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&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); 600b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, 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 867fb59c426SYoshinobu Inoue int isipv6; 868fb59c426SYoshinobu Inoue #endif 8694a91aa8fSMichael Tuexen u_int8_t incflagsav; 8704a91aa8fSMichael Tuexen u_char vflagsav; 8714a91aa8fSMichael Tuexen bool restoreflags; 8722c37256eSGarrett Wollman 873d8acd268SMark Johnston if (control != NULL) { 874d8acd268SMark Johnston /* TCP doesn't do control messages (rights, creds, etc) */ 875d8acd268SMark Johnston if (control->m_len) { 876d8acd268SMark Johnston m_freem(control); 8774287aa56SGleb Smirnoff return (EINVAL); 878d8acd268SMark Johnston } 879d8acd268SMark Johnston m_freem(control); /* empty control, just free it */ 880d8acd268SMark Johnston } 8814287aa56SGleb Smirnoff 8824287aa56SGleb Smirnoff inp = sotoinpcb(so); 8834287aa56SGleb Smirnoff KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 8844287aa56SGleb Smirnoff INP_WLOCK(inp); 88553af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 8864287aa56SGleb Smirnoff if (m != NULL && (flags & PRUS_NOTREADY) == 0) 8874287aa56SGleb Smirnoff m_freem(m); 8884287aa56SGleb Smirnoff INP_WUNLOCK(inp); 8894287aa56SGleb Smirnoff return (ECONNRESET); 8904287aa56SGleb Smirnoff } 8914287aa56SGleb Smirnoff 8924287aa56SGleb Smirnoff vflagsav = inp->inp_vflag; 8934287aa56SGleb Smirnoff incflagsav = inp->inp_inc.inc_flags; 8944287aa56SGleb Smirnoff restoreflags = false; 8954287aa56SGleb Smirnoff tp = intotcpcb(inp); 8964287aa56SGleb Smirnoff 8974287aa56SGleb Smirnoff NET_EPOCH_ENTER(et); 8987d2608a5SMark Johnston if ((flags & PRUS_OOB) != 0 && 8997d2608a5SMark Johnston (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0) 900d3b6c96bSRandall Stewart goto out; 9017d2608a5SMark Johnston 902888973f5SMichael Tuexen if (nam != NULL && tp->t_state < TCPS_SYN_SENT) { 903bd4a39ccSMark Johnston if (tp->t_state == TCPS_LISTEN) { 904bd4a39ccSMark Johnston error = EINVAL; 905bd4a39ccSMark Johnston goto out; 906bd4a39ccSMark Johnston } 907888973f5SMichael Tuexen switch (nam->sa_family) { 908888973f5SMichael Tuexen #ifdef INET 909888973f5SMichael Tuexen case AF_INET: 910888973f5SMichael Tuexen sinp = (struct sockaddr_in *)nam; 911888973f5SMichael Tuexen if (sinp->sin_len != sizeof(struct sockaddr_in)) { 912888973f5SMichael Tuexen error = EINVAL; 913888973f5SMichael Tuexen goto out; 914888973f5SMichael Tuexen } 915888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV6) != 0) { 916888973f5SMichael Tuexen error = EAFNOSUPPORT; 917888973f5SMichael Tuexen goto out; 918888973f5SMichael Tuexen } 919888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 920888973f5SMichael Tuexen error = EAFNOSUPPORT; 921888973f5SMichael Tuexen goto out; 922888973f5SMichael Tuexen } 923f903a308SMichael Tuexen if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) { 924f903a308SMichael Tuexen error = EACCES; 9252cf21ae5SRandall Stewart goto out; 9262cf21ae5SRandall Stewart } 927888973f5SMichael Tuexen if ((error = prison_remote_ip4(td->td_ucred, 9287d2608a5SMark Johnston &sinp->sin_addr))) 929888973f5SMichael Tuexen goto out; 930888973f5SMichael Tuexen #ifdef INET6 931888973f5SMichael Tuexen isipv6 = 0; 932888973f5SMichael Tuexen #endif 933888973f5SMichael Tuexen break; 934888973f5SMichael Tuexen #endif /* INET */ 935888973f5SMichael Tuexen #ifdef INET6 936888973f5SMichael Tuexen case AF_INET6: 937888973f5SMichael Tuexen { 9380ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 939888973f5SMichael Tuexen 9400ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 9410ecd976eSBjoern A. Zeeb if (sin6->sin6_len != sizeof(*sin6)) { 942888973f5SMichael Tuexen error = EINVAL; 943888973f5SMichael Tuexen goto out; 944888973f5SMichael Tuexen } 945e240ce42SMichael Tuexen if ((inp->inp_vflag & INP_IPV6PROTO) == 0) { 946e240ce42SMichael Tuexen error = EAFNOSUPPORT; 947e240ce42SMichael Tuexen goto out; 948e240ce42SMichael Tuexen } 9490ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 950888973f5SMichael Tuexen error = EAFNOSUPPORT; 951888973f5SMichael Tuexen goto out; 952888973f5SMichael Tuexen } 9530ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 954888973f5SMichael Tuexen #ifdef INET 955888973f5SMichael Tuexen if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 956888973f5SMichael Tuexen error = EINVAL; 957888973f5SMichael Tuexen goto out; 958888973f5SMichael Tuexen } 959888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV4) == 0) { 960888973f5SMichael Tuexen error = EAFNOSUPPORT; 961888973f5SMichael Tuexen goto out; 962888973f5SMichael Tuexen } 9634a91aa8fSMichael Tuexen restoreflags = true; 964888973f5SMichael Tuexen inp->inp_vflag &= ~INP_IPV6; 965888973f5SMichael Tuexen sinp = &sin; 9660ecd976eSBjoern A. Zeeb in6_sin6_2_sin(sinp, sin6); 967888973f5SMichael Tuexen if (IN_MULTICAST( 968888973f5SMichael Tuexen ntohl(sinp->sin_addr.s_addr))) { 969888973f5SMichael Tuexen error = EAFNOSUPPORT; 970888973f5SMichael Tuexen goto out; 971888973f5SMichael Tuexen } 972888973f5SMichael Tuexen if ((error = prison_remote_ip4(td->td_ucred, 9737d2608a5SMark Johnston &sinp->sin_addr))) 974888973f5SMichael Tuexen goto out; 975888973f5SMichael Tuexen isipv6 = 0; 976888973f5SMichael Tuexen #else /* !INET */ 977888973f5SMichael Tuexen error = EAFNOSUPPORT; 978888973f5SMichael Tuexen goto out; 979888973f5SMichael Tuexen #endif /* INET */ 980888973f5SMichael Tuexen } else { 981888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV6) == 0) { 982888973f5SMichael Tuexen error = EAFNOSUPPORT; 983888973f5SMichael Tuexen goto out; 984888973f5SMichael Tuexen } 9854a91aa8fSMichael Tuexen restoreflags = true; 986888973f5SMichael Tuexen inp->inp_vflag &= ~INP_IPV4; 987888973f5SMichael Tuexen inp->inp_inc.inc_flags |= INC_ISIPV6; 988888973f5SMichael Tuexen if ((error = prison_remote_ip6(td->td_ucred, 9897d2608a5SMark Johnston &sin6->sin6_addr))) 990888973f5SMichael Tuexen goto out; 991888973f5SMichael Tuexen isipv6 = 1; 992888973f5SMichael Tuexen } 993888973f5SMichael Tuexen break; 994888973f5SMichael Tuexen } 995888973f5SMichael Tuexen #endif /* INET6 */ 996888973f5SMichael Tuexen default: 997888973f5SMichael Tuexen error = EAFNOSUPPORT; 998888973f5SMichael Tuexen goto out; 999888973f5SMichael Tuexen } 1000888973f5SMichael Tuexen } 10012c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 100208af8aacSRandall Stewart if (tp->t_acktime == 0) 100308af8aacSRandall Stewart tp->t_acktime = ticks; 1004651e4e6aSGleb Smirnoff sbappendstream(&so->so_snd, m, flags); 10057d2608a5SMark Johnston m = NULL; 10062c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 1007bd4a39ccSMark Johnston KASSERT(tp->t_state == TCPS_CLOSED, 1008bd4a39ccSMark Johnston ("%s: tp %p is listening", __func__, tp)); 1009bd4a39ccSMark Johnston 10102c37256eSGarrett Wollman /* 10112c37256eSGarrett Wollman * Do implied connect if not yet connected, 10122c37256eSGarrett Wollman * initialize window to default value, and 10130c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 10142c37256eSGarrett Wollman */ 1015fb59c426SYoshinobu Inoue #ifdef INET6 1016fb59c426SYoshinobu Inoue if (isipv6) 1017b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 1018fb59c426SYoshinobu Inoue #endif /* INET6 */ 1019b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1020b287c6c7SBjoern A. Zeeb else 1021b287c6c7SBjoern A. Zeeb #endif 1022b287c6c7SBjoern A. Zeeb #ifdef INET 1023888973f5SMichael Tuexen error = tcp_connect(tp, 1024888973f5SMichael Tuexen (struct sockaddr *)sinp, td); 1025b287c6c7SBjoern A. Zeeb #endif 10264a91aa8fSMichael Tuexen /* 10274a91aa8fSMichael Tuexen * The bind operation in tcp_connect succeeded. We 10284a91aa8fSMichael Tuexen * no longer want to restore the flags if later 10294a91aa8fSMichael Tuexen * operations fail. 10304a91aa8fSMichael Tuexen */ 10314a91aa8fSMichael Tuexen if (error == 0 || inp->inp_lport != 0) 10324a91aa8fSMichael Tuexen restoreflags = false; 10334a91aa8fSMichael Tuexen 10347d2608a5SMark Johnston if (error) { 10357d2608a5SMark Johnston /* m is freed if PRUS_NOTREADY is unset. */ 10367d2608a5SMark Johnston sbflush(&so->so_snd); 10372c37256eSGarrett Wollman goto out; 10387d2608a5SMark Johnston } 1039c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) 1040c560df6fSPatrick Kelsey tcp_fastopen_connect(tp); 104118a75309SPatrick Kelsey else { 10422c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 10432c37256eSGarrett Wollman tcp_mss(tp, -1); 10442c37256eSGarrett Wollman } 1045c560df6fSPatrick Kelsey } 10462c37256eSGarrett Wollman if (flags & PRUS_EOF) { 10472c37256eSGarrett Wollman /* 10482c37256eSGarrett Wollman * Close the send side of the connection after 10492c37256eSGarrett Wollman * the data is sent. 10502c37256eSGarrett Wollman */ 10512c37256eSGarrett Wollman socantsendmore(so); 1052623dce13SRobert Watson tcp_usrclosed(tp); 10532c37256eSGarrett Wollman } 1054e854dd38SRandall Stewart if (TCPS_HAVEESTABLISHED(tp->t_state) && 1055e854dd38SRandall Stewart ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 1056e854dd38SRandall Stewart (tp->t_fbyte_out == 0) && 1057e854dd38SRandall Stewart (so->so_snd.sb_ccc > 0)) { 1058e854dd38SRandall Stewart tp->t_fbyte_out = ticks; 1059e854dd38SRandall Stewart if (tp->t_fbyte_out == 0) 1060e854dd38SRandall Stewart tp->t_fbyte_out = 1; 1061e854dd38SRandall Stewart if (tp->t_fbyte_out && tp->t_fbyte_in) 1062e854dd38SRandall Stewart tp->t_flags2 |= TF2_FBYTES_COMPLETE; 1063e854dd38SRandall Stewart } 10642cbcd3c1SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED) && 10652cbcd3c1SGleb Smirnoff !(flags & PRUS_NOTREADY)) { 1066b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 1067b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 1068f64dc2abSGleb Smirnoff error = tcp_output_nodrop(tp); 1069b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 1070b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 1071b0acefa8SBill Fenner } 10722c37256eSGarrett Wollman } else { 1073623dce13SRobert Watson /* 1074623dce13SRobert Watson * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 1075623dce13SRobert Watson */ 1076d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 10772c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 1078d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 10792c37256eSGarrett Wollman error = ENOBUFS; 10802c37256eSGarrett Wollman goto out; 10812c37256eSGarrett Wollman } 10822c37256eSGarrett Wollman /* 10832c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 10842c37256eSGarrett Wollman * the urgent pointer points to the last octet 10852c37256eSGarrett Wollman * of urgent data. We continue, however, 10862c37256eSGarrett Wollman * to consider it to indicate the first octet 10872c37256eSGarrett Wollman * of data past the urgent section. 10882c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 10892c37256eSGarrett Wollman */ 109008af8aacSRandall Stewart if (tp->t_acktime == 0) 109108af8aacSRandall Stewart tp->t_acktime = ticks; 1092651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_snd, m, flags); 1093d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 10947d2608a5SMark Johnston m = NULL; 1095ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 1096ef53690bSGarrett Wollman /* 1097ef53690bSGarrett Wollman * Do implied connect if not yet connected, 1098ef53690bSGarrett Wollman * initialize window to default value, and 10990c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 1100ef53690bSGarrett Wollman */ 110118a75309SPatrick Kelsey 1102c560df6fSPatrick Kelsey /* 1103c560df6fSPatrick Kelsey * Not going to contemplate SYN|URG 1104c560df6fSPatrick Kelsey */ 1105c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) 1106c560df6fSPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 1107fb59c426SYoshinobu Inoue #ifdef INET6 1108fb59c426SYoshinobu Inoue if (isipv6) 1109b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 1110fb59c426SYoshinobu Inoue #endif /* INET6 */ 1111b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1112b287c6c7SBjoern A. Zeeb else 1113b287c6c7SBjoern A. Zeeb #endif 1114b287c6c7SBjoern A. Zeeb #ifdef INET 1115888973f5SMichael Tuexen error = tcp_connect(tp, 1116888973f5SMichael Tuexen (struct sockaddr *)sinp, td); 1117b287c6c7SBjoern A. Zeeb #endif 11184a91aa8fSMichael Tuexen /* 11194a91aa8fSMichael Tuexen * The bind operation in tcp_connect succeeded. We 11204a91aa8fSMichael Tuexen * no longer want to restore the flags if later 11214a91aa8fSMichael Tuexen * operations fail. 11224a91aa8fSMichael Tuexen */ 11234a91aa8fSMichael Tuexen if (error == 0 || inp->inp_lport != 0) 11244a91aa8fSMichael Tuexen restoreflags = false; 11254a91aa8fSMichael Tuexen 11267d2608a5SMark Johnston if (error != 0) { 11277d2608a5SMark Johnston /* m is freed if PRUS_NOTREADY is unset. */ 11287d2608a5SMark Johnston sbflush(&so->so_snd); 1129ef53690bSGarrett Wollman goto out; 11307d2608a5SMark Johnston } 1131ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 1132ef53690bSGarrett Wollman tcp_mss(tp, -1); 1133623dce13SRobert Watson } 1134300fa232SGleb Smirnoff tp->snd_up = tp->snd_una + sbavail(&so->so_snd); 11357d2608a5SMark Johnston if ((flags & PRUS_NOTREADY) == 0) { 11362cdbfa66SPaul Saab tp->t_flags |= TF_FORCEDATA; 1137f64dc2abSGleb Smirnoff error = tcp_output_nodrop(tp); 11382cdbfa66SPaul Saab tp->t_flags &= ~TF_FORCEDATA; 11392c37256eSGarrett Wollman } 11402cbcd3c1SGleb Smirnoff } 11412529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, NULL, 11422529f56eSJonathan T. Looney &inp->inp_socket->so_rcv, 11432529f56eSJonathan T. Looney &inp->inp_socket->so_snd, 11442529f56eSJonathan T. Looney TCP_LOG_USERSEND, error, 11452529f56eSJonathan T. Looney 0, NULL, false); 11467d2608a5SMark Johnston 1147d1401c90SRobert Watson out: 11484a91aa8fSMichael Tuexen /* 11497d2608a5SMark Johnston * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is 11507d2608a5SMark Johnston * responsible for freeing memory. 11517d2608a5SMark Johnston */ 11527d2608a5SMark Johnston if (m != NULL && (flags & PRUS_NOTREADY) == 0) 11537d2608a5SMark Johnston m_freem(m); 11547d2608a5SMark Johnston 11557d2608a5SMark Johnston /* 11564a91aa8fSMichael Tuexen * If the request was unsuccessful and we changed flags, 11574a91aa8fSMichael Tuexen * restore the original flags. 11584a91aa8fSMichael Tuexen */ 11594a91aa8fSMichael Tuexen if (error != 0 && restoreflags) { 11604a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 11614a91aa8fSMichael Tuexen inp->inp_inc.inc_flags = incflagsav; 11624a91aa8fSMichael Tuexen } 11635d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB : 11645d06879aSGeorge V. Neville-Neil ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 1165f64dc2abSGleb Smirnoff error = tcp_unlock_or_drop(tp, error); 116697a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 116773fddedaSPeter Grehan return (error); 11682c37256eSGarrett Wollman } 11692c37256eSGarrett Wollman 11702cbcd3c1SGleb Smirnoff static int 11712cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count) 11722cbcd3c1SGleb Smirnoff { 1173109eb549SGleb Smirnoff struct epoch_tracker et; 11742cbcd3c1SGleb Smirnoff struct inpcb *inp; 11752cbcd3c1SGleb Smirnoff struct tcpcb *tp; 11762cbcd3c1SGleb Smirnoff int error; 11772cbcd3c1SGleb Smirnoff 11782cbcd3c1SGleb Smirnoff inp = sotoinpcb(so); 11792cbcd3c1SGleb Smirnoff INP_WLOCK(inp); 118053af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 11812cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 118282334850SJohn Baldwin mb_free_notready(m, count); 11832cbcd3c1SGleb Smirnoff return (ECONNRESET); 11842cbcd3c1SGleb Smirnoff } 11852cbcd3c1SGleb Smirnoff tp = intotcpcb(inp); 11862cbcd3c1SGleb Smirnoff 11872cbcd3c1SGleb Smirnoff SOCKBUF_LOCK(&so->so_snd); 11882cbcd3c1SGleb Smirnoff error = sbready(&so->so_snd, m, count); 11892cbcd3c1SGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 1190f64dc2abSGleb Smirnoff if (error) { 11912cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 1192f64dc2abSGleb Smirnoff return (error); 1193f64dc2abSGleb Smirnoff } 1194f64dc2abSGleb Smirnoff NET_EPOCH_ENTER(et); 1195f64dc2abSGleb Smirnoff error = tcp_output_unlock(tp); 1196f64dc2abSGleb Smirnoff NET_EPOCH_EXIT(et); 11972cbcd3c1SGleb Smirnoff 11982cbcd3c1SGleb Smirnoff return (error); 11992cbcd3c1SGleb Smirnoff } 12002cbcd3c1SGleb Smirnoff 12012c37256eSGarrett Wollman /* 1202a152f8a3SRobert Watson * Abort the TCP. Drop the connection abruptly. 12032c37256eSGarrett Wollman */ 1204ac45e92fSRobert Watson static void 12052c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 12062c37256eSGarrett Wollman { 1207f76fcf6dSJeffrey Hsu struct inpcb *inp; 1208a152f8a3SRobert Watson struct tcpcb *tp = NULL; 12096573d758SMatt Macy struct epoch_tracker et; 1210c78cbc7bSRobert Watson 1211ac45e92fSRobert Watson inp = sotoinpcb(so); 1212c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 1213c78cbc7bSRobert Watson 121497a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 12158501a69cSRobert Watson INP_WLOCK(inp); 1216c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 1217c78cbc7bSRobert Watson ("tcp_usr_abort: inp_socket == NULL")); 1218c78cbc7bSRobert Watson 1219c78cbc7bSRobert Watson /* 1220a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, drop. 1221c78cbc7bSRobert Watson */ 122253af6903SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED)) { 1223c78cbc7bSRobert Watson tp = intotcpcb(inp); 12248fa799bdSJonathan T. Looney tp = tcp_drop(tp, ECONNABORTED); 12258fa799bdSJonathan T. Looney if (tp == NULL) 12268fa799bdSJonathan T. Looney goto dropped; 12275d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ABORT); 1228c78cbc7bSRobert Watson } 1229ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1230d8596171SGleb Smirnoff soref(so); 1231ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1232a152f8a3SRobert Watson } 12338501a69cSRobert Watson INP_WUNLOCK(inp); 12348fa799bdSJonathan T. Looney dropped: 123597a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 1236a152f8a3SRobert Watson } 1237a152f8a3SRobert Watson 1238a152f8a3SRobert Watson /* 1239a152f8a3SRobert Watson * TCP socket is closed. Start friendly disconnect. 1240a152f8a3SRobert Watson */ 1241a152f8a3SRobert Watson static void 1242a152f8a3SRobert Watson tcp_usr_close(struct socket *so) 1243a152f8a3SRobert Watson { 1244a152f8a3SRobert Watson struct inpcb *inp; 1245a152f8a3SRobert Watson struct tcpcb *tp = NULL; 12466573d758SMatt Macy struct epoch_tracker et; 1247a152f8a3SRobert Watson 1248a152f8a3SRobert Watson inp = sotoinpcb(so); 1249a152f8a3SRobert Watson KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 1250a152f8a3SRobert Watson 125197a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 12528501a69cSRobert Watson INP_WLOCK(inp); 1253a152f8a3SRobert Watson KASSERT(inp->inp_socket != NULL, 1254a152f8a3SRobert Watson ("tcp_usr_close: inp_socket == NULL")); 1255a152f8a3SRobert Watson 1256a152f8a3SRobert Watson /* 1257a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, initiate 1258a152f8a3SRobert Watson * a disconnect. 1259a152f8a3SRobert Watson */ 126053af6903SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED)) { 1261a152f8a3SRobert Watson tp = intotcpcb(inp); 126274703901SGleb Smirnoff tp->t_flags |= TF_CLOSED; 1263a152f8a3SRobert Watson tcp_disconnect(tp); 12645d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CLOSE); 1265a152f8a3SRobert Watson } 1266ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1267d8596171SGleb Smirnoff soref(so); 1268ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1269a152f8a3SRobert Watson } 12708501a69cSRobert Watson INP_WUNLOCK(inp); 127197a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 12722c37256eSGarrett Wollman } 12732c37256eSGarrett Wollman 1274d3b6c96bSRandall Stewart static int 1275d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags) 1276d3b6c96bSRandall Stewart { 1277d3b6c96bSRandall Stewart /* 1278d3b6c96bSRandall Stewart * If the specific TCP stack has a pru_options 1279d3b6c96bSRandall Stewart * specified then it does not always support 1280d3b6c96bSRandall Stewart * all the PRU_XX options and we must ask it. 1281d3b6c96bSRandall Stewart * If the function is not specified then all 1282d3b6c96bSRandall Stewart * of the PRU_XX options are supported. 1283d3b6c96bSRandall Stewart */ 1284d3b6c96bSRandall Stewart int ret = 0; 1285d3b6c96bSRandall Stewart 1286d3b6c96bSRandall Stewart if (tp->t_fb->tfb_pru_options) { 1287d3b6c96bSRandall Stewart ret = (*tp->t_fb->tfb_pru_options)(tp, flags); 1288d3b6c96bSRandall Stewart } 1289d3b6c96bSRandall Stewart return (ret); 1290d3b6c96bSRandall Stewart } 1291d3b6c96bSRandall Stewart 12922c37256eSGarrett Wollman /* 12932c37256eSGarrett Wollman * Receive out-of-band data. 12942c37256eSGarrett Wollman */ 12952c37256eSGarrett Wollman static int 12962c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 12972c37256eSGarrett Wollman { 12982c37256eSGarrett Wollman int error = 0; 1299f76fcf6dSJeffrey Hsu struct inpcb *inp; 1300623dce13SRobert Watson struct tcpcb *tp = NULL; 13012c37256eSGarrett Wollman 1302623dce13SRobert Watson inp = sotoinpcb(so); 1303623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 13048501a69cSRobert Watson INP_WLOCK(inp); 130553af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 130621367f63SSam Leffler error = ECONNRESET; 1307623dce13SRobert Watson goto out; 1308623dce13SRobert Watson } 1309623dce13SRobert Watson tp = intotcpcb(inp); 1310d3b6c96bSRandall Stewart error = tcp_pru_options_support(tp, PRUS_OOB); 1311d3b6c96bSRandall Stewart if (error) { 1312d3b6c96bSRandall Stewart goto out; 1313d3b6c96bSRandall Stewart } 13142c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 1315c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 13164cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 13174cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 13182c37256eSGarrett Wollman error = EINVAL; 13192c37256eSGarrett Wollman goto out; 13202c37256eSGarrett Wollman } 13212c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 13222c37256eSGarrett Wollman error = EWOULDBLOCK; 13232c37256eSGarrett Wollman goto out; 13242c37256eSGarrett Wollman } 13252c37256eSGarrett Wollman m->m_len = 1; 13262c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 13272c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 13282c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1329623dce13SRobert Watson 1330623dce13SRobert Watson out: 13315d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVOOB); 13328501a69cSRobert Watson INP_WUNLOCK(inp); 1333623dce13SRobert Watson return (error); 13342c37256eSGarrett Wollman } 13352c37256eSGarrett Wollman 1336b287c6c7SBjoern A. Zeeb #ifdef INET 1337e7d02be1SGleb Smirnoff struct protosw tcp_protosw = { 1338e7d02be1SGleb Smirnoff .pr_type = SOCK_STREAM, 1339e7d02be1SGleb Smirnoff .pr_protocol = IPPROTO_TCP, 1340e7d02be1SGleb Smirnoff .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD | 1341e7d02be1SGleb Smirnoff PR_CAPATTACH, 1342e7d02be1SGleb Smirnoff .pr_ctloutput = tcp_ctloutput, 1343e7d02be1SGleb Smirnoff .pr_abort = tcp_usr_abort, 1344e7d02be1SGleb Smirnoff .pr_accept = tcp_usr_accept, 1345e7d02be1SGleb Smirnoff .pr_attach = tcp_usr_attach, 1346e7d02be1SGleb Smirnoff .pr_bind = tcp_usr_bind, 1347e7d02be1SGleb Smirnoff .pr_connect = tcp_usr_connect, 1348e7d02be1SGleb Smirnoff .pr_control = in_control, 1349e7d02be1SGleb Smirnoff .pr_detach = tcp_usr_detach, 1350e7d02be1SGleb Smirnoff .pr_disconnect = tcp_usr_disconnect, 1351e7d02be1SGleb Smirnoff .pr_listen = tcp_usr_listen, 1352e7d02be1SGleb Smirnoff .pr_peeraddr = in_getpeeraddr, 1353e7d02be1SGleb Smirnoff .pr_rcvd = tcp_usr_rcvd, 1354e7d02be1SGleb Smirnoff .pr_rcvoob = tcp_usr_rcvoob, 1355e7d02be1SGleb Smirnoff .pr_send = tcp_usr_send, 1356e7d02be1SGleb Smirnoff .pr_ready = tcp_usr_ready, 1357e7d02be1SGleb Smirnoff .pr_shutdown = tcp_usr_shutdown, 1358e7d02be1SGleb Smirnoff .pr_sockaddr = in_getsockaddr, 1359e7d02be1SGleb Smirnoff .pr_sosetlabel = in_pcbsosetlabel, 1360e7d02be1SGleb Smirnoff .pr_close = tcp_usr_close, 13612c37256eSGarrett Wollman }; 1362b287c6c7SBjoern A. Zeeb #endif /* INET */ 1363df8bae1dSRodney W. Grimes 1364fb59c426SYoshinobu Inoue #ifdef INET6 1365e7d02be1SGleb Smirnoff struct protosw tcp6_protosw = { 1366e7d02be1SGleb Smirnoff .pr_type = SOCK_STREAM, 1367e7d02be1SGleb Smirnoff .pr_protocol = IPPROTO_TCP, 1368e7d02be1SGleb Smirnoff .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD | 1369e7d02be1SGleb Smirnoff PR_CAPATTACH, 1370e7d02be1SGleb Smirnoff .pr_ctloutput = tcp_ctloutput, 1371e7d02be1SGleb Smirnoff .pr_abort = tcp_usr_abort, 1372e7d02be1SGleb Smirnoff .pr_accept = tcp6_usr_accept, 1373e7d02be1SGleb Smirnoff .pr_attach = tcp_usr_attach, 1374e7d02be1SGleb Smirnoff .pr_bind = tcp6_usr_bind, 1375e7d02be1SGleb Smirnoff .pr_connect = tcp6_usr_connect, 1376e7d02be1SGleb Smirnoff .pr_control = in6_control, 1377e7d02be1SGleb Smirnoff .pr_detach = tcp_usr_detach, 1378e7d02be1SGleb Smirnoff .pr_disconnect = tcp_usr_disconnect, 1379e7d02be1SGleb Smirnoff .pr_listen = tcp6_usr_listen, 1380e7d02be1SGleb Smirnoff .pr_peeraddr = in6_mapped_peeraddr, 1381e7d02be1SGleb Smirnoff .pr_rcvd = tcp_usr_rcvd, 1382e7d02be1SGleb Smirnoff .pr_rcvoob = tcp_usr_rcvoob, 1383e7d02be1SGleb Smirnoff .pr_send = tcp_usr_send, 1384e7d02be1SGleb Smirnoff .pr_ready = tcp_usr_ready, 1385e7d02be1SGleb Smirnoff .pr_shutdown = tcp_usr_shutdown, 1386e7d02be1SGleb Smirnoff .pr_sockaddr = in6_mapped_sockaddr, 1387e7d02be1SGleb Smirnoff .pr_sosetlabel = in_pcbsosetlabel, 1388e7d02be1SGleb Smirnoff .pr_close = tcp_usr_close, 1389fb59c426SYoshinobu Inoue }; 1390fb59c426SYoshinobu Inoue #endif /* INET6 */ 1391fb59c426SYoshinobu Inoue 1392b287c6c7SBjoern A. Zeeb #ifdef INET 1393a0292f23SGarrett Wollman /* 1394a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 1395a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 13965200e00eSIan Dowse * port number if needed. Call in_pcbconnect_setup to do the routing and 13975200e00eSIan Dowse * to choose a local host address (interface). If there is an existing 13985200e00eSIan Dowse * incarnation of the same connection in TIME-WAIT state and if the remote 13995200e00eSIan Dowse * host was sending CC options and if the connection duration was < MSL, then 1400a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 1401a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 1402a0292f23SGarrett Wollman */ 14030312fbe9SPoul-Henning Kamp static int 1404ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1405a0292f23SGarrett Wollman { 14069eb0e832SGleb Smirnoff struct inpcb *inp = tptoinpcb(tp), *oinp; 14079eb0e832SGleb Smirnoff struct socket *so = tptosocket(tp); 14085200e00eSIan Dowse struct in_addr laddr; 14095200e00eSIan Dowse u_short lport; 1410c3229e05SDavid Greenman int error; 1411a0292f23SGarrett Wollman 1412c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 14138501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1414*76f1499fSGleb Smirnoff 1415fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1416a0292f23SGarrett Wollman /* 1417a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 1418a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 1419a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 1420a0292f23SGarrett Wollman */ 14215200e00eSIan Dowse laddr = inp->inp_laddr; 14225200e00eSIan Dowse lport = inp->inp_lport; 14235200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1424b0330ed9SPawel Jakub Dawidek &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 14255200e00eSIan Dowse if (error && oinp == NULL) 1426fa046d87SRobert Watson goto out; 1427fa046d87SRobert Watson if (oinp) { 1428fa046d87SRobert Watson error = EADDRINUSE; 1429fa046d87SRobert Watson goto out; 1430fa046d87SRobert Watson } 143125102351SMike Karels /* Handle initial bind if it hadn't been done in advance. */ 143225102351SMike Karels if (inp->inp_lport == 0) { 143325102351SMike Karels inp->inp_lport = lport; 143425102351SMike Karels if (in_pcbinshash(inp) != 0) { 143525102351SMike Karels inp->inp_lport = 0; 143625102351SMike Karels error = EAGAIN; 143725102351SMike Karels goto out; 143825102351SMike Karels } 143925102351SMike Karels } 14405200e00eSIan Dowse inp->inp_laddr = laddr; 144115bd2b43SDavid Greenman in_pcbrehash(inp); 1442fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1443a0292f23SGarrett Wollman 1444087b55eaSAndre Oppermann /* 1445087b55eaSAndre Oppermann * Compute window scaling to request: 1446087b55eaSAndre Oppermann * Scale to fit into sweet spot. See tcp_syncache.c. 1447087b55eaSAndre Oppermann * XXX: This should move to tcp_output(). 1448087b55eaSAndre Oppermann */ 1449a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 14509b3bc6bfSMike Silbersack (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1451a0292f23SGarrett Wollman tp->request_r_scale++; 1452a0292f23SGarrett Wollman 1453a0292f23SGarrett Wollman soisconnecting(so); 145478b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 145557f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 14568e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 14578e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 14588e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1459a0292f23SGarrett Wollman tcp_sendseqinit(tp); 1460a45d2726SAndras Olah 1461a0292f23SGarrett Wollman return 0; 1462fa046d87SRobert Watson 1463fa046d87SRobert Watson out: 1464fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1465fa046d87SRobert Watson return (error); 1466a0292f23SGarrett Wollman } 1467b287c6c7SBjoern A. Zeeb #endif /* INET */ 1468a0292f23SGarrett Wollman 1469fb59c426SYoshinobu Inoue #ifdef INET6 1470fb59c426SYoshinobu Inoue static int 1471ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1472fb59c426SYoshinobu Inoue { 14739eb0e832SGleb Smirnoff struct inpcb *inp = tptoinpcb(tp); 1474fb59c426SYoshinobu Inoue int error; 1475fb59c426SYoshinobu Inoue 14768501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1477623dce13SRobert Watson 1478*76f1499fSGleb Smirnoff INP_HASH_WLOCK(&V_tcbinfo); 1479a7e201bbSAndrey V. Elsukov error = in6_pcbconnect(inp, nam, td->td_ucred); 1480fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1481*76f1499fSGleb Smirnoff if (error != 0) 1482*76f1499fSGleb Smirnoff return (error); 1483fb59c426SYoshinobu Inoue 1484fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1485fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1486970caf60SBjoern A. Zeeb (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1487fb59c426SYoshinobu Inoue tp->request_r_scale++; 1488fb59c426SYoshinobu Inoue 1489a7e201bbSAndrey V. Elsukov soisconnecting(inp->inp_socket); 149078b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 149157f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 14928e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 14938e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 14948e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1495fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1496fb59c426SYoshinobu Inoue 1497*76f1499fSGleb Smirnoff return (0); 1498fb59c426SYoshinobu Inoue } 1499fb59c426SYoshinobu Inoue #endif /* INET6 */ 1500fb59c426SYoshinobu Inoue 1501cfe8b629SGarrett Wollman /* 1502b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1503b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1504b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1505b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1506b8af5dfaSRobert Watson * from Linux. 1507b8af5dfaSRobert Watson */ 1508b8af5dfaSRobert Watson static void 1509ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1510b8af5dfaSRobert Watson { 1511b8af5dfaSRobert Watson 15129eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 1513b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1514b8af5dfaSRobert Watson 1515b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1516b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1517b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 15183529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 1519b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1520b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1521b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1522b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1523b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1524b8af5dfaSRobert Watson } 15253f169c54SRichard Scheffenegger if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) 15265a17b6adSMichael Tuexen ti->tcpi_options |= TCPI_OPT_ECN; 15271baaf834SBruce M Simpson 152843d94734SJohn Baldwin ti->tcpi_rto = tp->t_rxtcur * tick; 15293ac12506SJonathan T. Looney ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick; 15301baaf834SBruce M Simpson ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 15311baaf834SBruce M Simpson ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 15321baaf834SBruce M Simpson 1533b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1534b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1535b8af5dfaSRobert Watson 1536b8af5dfaSRobert Watson /* 1537b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1538b8af5dfaSRobert Watson */ 1539c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1540535fbad6SKip Macy ti->tcpi_rcv_nxt = tp->rcv_nxt; 1541b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 15421c18314dSAndre Oppermann ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1543535fbad6SKip Macy ti->tcpi_snd_nxt = tp->snd_nxt; 154443d94734SJohn Baldwin ti->tcpi_snd_mss = tp->t_maxseg; 154543d94734SJohn Baldwin ti->tcpi_rcv_mss = tp->t_maxseg; 1546f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1547f5d34df5SGeorge V. Neville-Neil ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1548f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1549a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD 1550a6456410SNavdeep Parhar if (tp->t_flags & TF_TOE) { 1551a6456410SNavdeep Parhar ti->tcpi_options |= TCPI_OPT_TOE; 1552a6456410SNavdeep Parhar tcp_offload_tcp_info(tp, ti); 1553a6456410SNavdeep Parhar } 1554a6456410SNavdeep Parhar #endif 155522c81cc5SRichard Scheffenegger /* 155622c81cc5SRichard Scheffenegger * AccECN related counters. 155722c81cc5SRichard Scheffenegger */ 155822c81cc5SRichard Scheffenegger if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) == 155922c81cc5SRichard Scheffenegger (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) 156022c81cc5SRichard Scheffenegger /* 156122c81cc5SRichard Scheffenegger * Internal counter starts at 5 for AccECN 156222c81cc5SRichard Scheffenegger * but 0 for RFC3168 ECN. 156322c81cc5SRichard Scheffenegger */ 156422c81cc5SRichard Scheffenegger ti->tcpi_delivered_ce = tp->t_scep - 5; 156522c81cc5SRichard Scheffenegger else 156622c81cc5SRichard Scheffenegger ti->tcpi_delivered_ce = tp->t_scep; 156722c81cc5SRichard Scheffenegger ti->tcpi_received_ce = tp->t_rcep; 1568b8af5dfaSRobert Watson } 1569b8af5dfaSRobert Watson 1570b8af5dfaSRobert Watson /* 15711e8f5ffaSRobert Watson * tcp_ctloutput() must drop the inpcb lock before performing copyin on 15721e8f5ffaSRobert Watson * socket option arguments. When it re-acquires the lock after the copy, it 15731e8f5ffaSRobert Watson * has to revalidate that the connection is still valid for the socket 15741e8f5ffaSRobert Watson * option. 1575cfe8b629SGarrett Wollman */ 1576bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \ 15778501a69cSRobert Watson INP_WLOCK(inp); \ 157853af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { \ 15798501a69cSRobert Watson INP_WUNLOCK(inp); \ 1580bac5bedfSConrad Meyer cleanup; \ 15811e8f5ffaSRobert Watson return (ECONNRESET); \ 15821e8f5ffaSRobert Watson } \ 15831e8f5ffaSRobert Watson tp = intotcpcb(inp); \ 15841e8f5ffaSRobert Watson } while(0) 1585bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */) 15861e8f5ffaSRobert Watson 1587fd7daa72SMichael Tuexen int 1588fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt) 1589df8bae1dSRodney W. Grimes { 1590fd7daa72SMichael Tuexen struct socket *so = inp->inp_socket; 1591fc4d53ccSGleb Smirnoff struct tcpcb *tp = intotcpcb(inp); 1592fc4d53ccSGleb Smirnoff int error = 0; 1593df8bae1dSRodney W. Grimes 1594fc4d53ccSGleb Smirnoff MPASS(sopt->sopt_dir == SOPT_SET); 15953b3c08c1SMichael Tuexen INP_WLOCK_ASSERT(inp); 159653af6903SGleb Smirnoff KASSERT((inp->inp_flags & INP_DROPPED) == 0, 1597fd7daa72SMichael Tuexen ("inp_flags == %x", inp->inp_flags)); 1598fd7daa72SMichael Tuexen KASSERT(so != NULL, ("inp_socket == NULL")); 1599fc4d53ccSGleb Smirnoff 1600cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 16013b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 1602fb59c426SYoshinobu Inoue #ifdef INET6 1603de156263SGleb Smirnoff if (inp->inp_vflag & INP_IPV6PROTO) 1604fd7daa72SMichael Tuexen error = ip6_ctloutput(so, sopt); 1605de156263SGleb Smirnoff #endif 1606de156263SGleb Smirnoff #if defined(INET6) && defined(INET) 1607de156263SGleb Smirnoff else 1608de156263SGleb Smirnoff #endif 1609de156263SGleb Smirnoff #ifdef INET 1610fd7daa72SMichael Tuexen error = ip_ctloutput(so, sopt); 1611de156263SGleb Smirnoff #endif 16125dff1c38SMichael Tuexen /* 1613de156263SGleb Smirnoff * When an IP-level socket option affects TCP, pass control 1614de156263SGleb Smirnoff * down to stack tfb_tcp_ctloutput, otherwise return what 1615de156263SGleb Smirnoff * IP level returned. 16165dff1c38SMichael Tuexen */ 1617de156263SGleb Smirnoff switch (sopt->sopt_level) { 1618de156263SGleb Smirnoff #ifdef INET6 1619de156263SGleb Smirnoff case IPPROTO_IPV6: 1620de156263SGleb Smirnoff if ((inp->inp_vflag & INP_IPV6PROTO) == 0) 1621de156263SGleb Smirnoff return (error); 1622de156263SGleb Smirnoff switch (sopt->sopt_name) { 1623de156263SGleb Smirnoff case IPV6_TCLASS: 1624de156263SGleb Smirnoff /* Notify tcp stacks that care (e.g. RACK). */ 1625de156263SGleb Smirnoff break; 1626de156263SGleb Smirnoff case IPV6_USE_MIN_MTU: 1627f581a26eSGleb Smirnoff /* Update t_maxseg accordingly. */ 1628f581a26eSGleb Smirnoff break; 1629de156263SGleb Smirnoff default: 1630de156263SGleb Smirnoff return (error); 16315dff1c38SMichael Tuexen } 1632de156263SGleb Smirnoff break; 1633b287c6c7SBjoern A. Zeeb #endif 1634b287c6c7SBjoern A. Zeeb #ifdef INET 1635de156263SGleb Smirnoff case IPPROTO_IP: 1636de156263SGleb Smirnoff switch (sopt->sopt_name) { 1637de156263SGleb Smirnoff case IP_TOS: 16383b0ee680SRichard Scheffenegger inp->inp_ip_tos &= ~IPTOS_ECN_MASK; 16393b0ee680SRichard Scheffenegger break; 1640de156263SGleb Smirnoff case IP_TTL: 1641de156263SGleb Smirnoff /* Notify tcp stacks that care (e.g. RACK). */ 1642de156263SGleb Smirnoff break; 1643de156263SGleb Smirnoff default: 1644df8bae1dSRodney W. Grimes return (error); 1645de156263SGleb Smirnoff } 1646de156263SGleb Smirnoff break; 1647de156263SGleb Smirnoff #endif 1648de156263SGleb Smirnoff default: 1649de156263SGleb Smirnoff return (error); 1650de156263SGleb Smirnoff } 16513b3c08c1SMichael Tuexen INP_WLOCK(inp); 165253af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 16533b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 16543b3c08c1SMichael Tuexen return (ECONNRESET); 16553b3c08c1SMichael Tuexen } 1656fc4d53ccSGleb Smirnoff } else if (sopt->sopt_name == TCP_FUNCTION_BLK) { 1657fc4d53ccSGleb Smirnoff /* 1658fc4d53ccSGleb Smirnoff * Protect the TCP option TCP_FUNCTION_BLK so 1659fc4d53ccSGleb Smirnoff * that a sub-function can *never* overwrite this. 1660fc4d53ccSGleb Smirnoff */ 1661fc4d53ccSGleb Smirnoff struct tcp_function_set fsn; 1662fc4d53ccSGleb Smirnoff struct tcp_function_block *blk; 1663fc4d53ccSGleb Smirnoff 16643b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 1665fc4d53ccSGleb Smirnoff error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn); 1666fc4d53ccSGleb Smirnoff if (error) 1667fc4d53ccSGleb Smirnoff return (error); 1668fc4d53ccSGleb Smirnoff 166968cea2b1SJohn Baldwin INP_WLOCK(inp); 167053af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 16718501a69cSRobert Watson INP_WUNLOCK(inp); 16721e8f5ffaSRobert Watson return (ECONNRESET); 1673623dce13SRobert Watson } 167455bceb1eSRandall Stewart tp = intotcpcb(inp); 1675fc4d53ccSGleb Smirnoff 167655bceb1eSRandall Stewart blk = find_and_ref_tcp_functions(&fsn); 167755bceb1eSRandall Stewart if (blk == NULL) { 167855bceb1eSRandall Stewart INP_WUNLOCK(inp); 167955bceb1eSRandall Stewart return (ENOENT); 168055bceb1eSRandall Stewart } 1681587d67c0SRandall Stewart if (tp->t_fb == blk) { 1682587d67c0SRandall Stewart /* You already have this */ 1683587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1684587d67c0SRandall Stewart INP_WUNLOCK(inp); 1685587d67c0SRandall Stewart return (0); 1686587d67c0SRandall Stewart } 1687587d67c0SRandall Stewart if (tp->t_state != TCPS_CLOSED) { 1688587d67c0SRandall Stewart /* 1689587d67c0SRandall Stewart * The user has advanced the state 1690587d67c0SRandall Stewart * past the initial point, we may not 1691587d67c0SRandall Stewart * be able to switch. 1692587d67c0SRandall Stewart */ 1693587d67c0SRandall Stewart if (blk->tfb_tcp_handoff_ok != NULL) { 1694587d67c0SRandall Stewart /* 1695587d67c0SRandall Stewart * Does the stack provide a 1696587d67c0SRandall Stewart * query mechanism, if so it may 1697587d67c0SRandall Stewart * still be possible? 1698587d67c0SRandall Stewart */ 1699587d67c0SRandall Stewart error = (*blk->tfb_tcp_handoff_ok)(tp); 1700c6c0be27SMichael Tuexen } else 1701c6c0be27SMichael Tuexen error = EINVAL; 1702587d67c0SRandall Stewart if (error) { 1703587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1704587d67c0SRandall Stewart INP_WUNLOCK(inp); 1705587d67c0SRandall Stewart return(error); 1706587d67c0SRandall Stewart } 1707587d67c0SRandall Stewart } 170855bceb1eSRandall Stewart if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) { 170955bceb1eSRandall Stewart refcount_release(&blk->tfb_refcnt); 171055bceb1eSRandall Stewart INP_WUNLOCK(inp); 171155bceb1eSRandall Stewart return (ENOENT); 171255bceb1eSRandall Stewart } 171355bceb1eSRandall Stewart /* 171455bceb1eSRandall Stewart * Release the old refcnt, the 1715587d67c0SRandall Stewart * lookup acquired a ref on the 1716587d67c0SRandall Stewart * new one already. 171755bceb1eSRandall Stewart */ 1718587d67c0SRandall Stewart if (tp->t_fb->tfb_tcp_fb_fini) { 1719086a3556SAndrew Gallatin struct epoch_tracker et; 1720587d67c0SRandall Stewart /* 1721587d67c0SRandall Stewart * Tell the stack to cleanup with 0 i.e. 1722587d67c0SRandall Stewart * the tcb is not going away. 1723587d67c0SRandall Stewart */ 1724086a3556SAndrew Gallatin NET_EPOCH_ENTER(et); 1725587d67c0SRandall Stewart (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0); 1726086a3556SAndrew Gallatin NET_EPOCH_EXIT(et); 1727587d67c0SRandall Stewart } 17283ee9c3c4SRandall Stewart #ifdef TCPHPTS 17293ee9c3c4SRandall Stewart /* Assure that we are not on any hpts */ 17309eb0e832SGleb Smirnoff tcp_hpts_remove(tptoinpcb(tp)); 17313ee9c3c4SRandall Stewart #endif 17323ee9c3c4SRandall Stewart if (blk->tfb_tcp_fb_init) { 17333ee9c3c4SRandall Stewart error = (*blk->tfb_tcp_fb_init)(tp); 17343ee9c3c4SRandall Stewart if (error) { 17353ee9c3c4SRandall Stewart refcount_release(&blk->tfb_refcnt); 17363ee9c3c4SRandall Stewart if (tp->t_fb->tfb_tcp_fb_init) { 17373ee9c3c4SRandall Stewart if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) { 17383ee9c3c4SRandall Stewart /* Fall back failed, drop the connection */ 17393ee9c3c4SRandall Stewart INP_WUNLOCK(inp); 1740fd7daa72SMichael Tuexen soabort(so); 17413ee9c3c4SRandall Stewart return (error); 17423ee9c3c4SRandall Stewart } 17433ee9c3c4SRandall Stewart } 17443ee9c3c4SRandall Stewart goto err_out; 17453ee9c3c4SRandall Stewart } 17463ee9c3c4SRandall Stewart } 174755bceb1eSRandall Stewart refcount_release(&tp->t_fb->tfb_refcnt); 174855bceb1eSRandall Stewart tp->t_fb = blk; 174955bceb1eSRandall Stewart #ifdef TCP_OFFLOAD 175055bceb1eSRandall Stewart if (tp->t_flags & TF_TOE) { 175155bceb1eSRandall Stewart tcp_offload_ctloutput(tp, sopt->sopt_dir, 175255bceb1eSRandall Stewart sopt->sopt_name); 175355bceb1eSRandall Stewart } 175455bceb1eSRandall Stewart #endif 17553ee9c3c4SRandall Stewart err_out: 175655bceb1eSRandall Stewart INP_WUNLOCK(inp); 175755bceb1eSRandall Stewart return (error); 1758fc4d53ccSGleb Smirnoff } 1759fc4d53ccSGleb Smirnoff 17603b3c08c1SMichael Tuexen /* Pass in the INP locked, callee must unlock it. */ 17613b3c08c1SMichael Tuexen return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt)); 1762fc4d53ccSGleb Smirnoff } 1763fc4d53ccSGleb Smirnoff 1764fc4d53ccSGleb Smirnoff static int 1765fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt) 1766fc4d53ccSGleb Smirnoff { 1767fd7daa72SMichael Tuexen struct socket *so = inp->inp_socket; 1768fd7daa72SMichael Tuexen struct tcpcb *tp = intotcpcb(inp); 1769fc4d53ccSGleb Smirnoff int error = 0; 1770fc4d53ccSGleb Smirnoff 1771fc4d53ccSGleb Smirnoff MPASS(sopt->sopt_dir == SOPT_GET); 17723b3c08c1SMichael Tuexen INP_WLOCK_ASSERT(inp); 177353af6903SGleb Smirnoff KASSERT((inp->inp_flags & INP_DROPPED) == 0, 1774fd7daa72SMichael Tuexen ("inp_flags == %x", inp->inp_flags)); 1775fd7daa72SMichael Tuexen KASSERT(so != NULL, ("inp_socket == NULL")); 1776fc4d53ccSGleb Smirnoff 1777fc4d53ccSGleb Smirnoff if (sopt->sopt_level != IPPROTO_TCP) { 17783b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 1779fc4d53ccSGleb Smirnoff #ifdef INET6 1780fc4d53ccSGleb Smirnoff if (inp->inp_vflag & INP_IPV6PROTO) 1781fd7daa72SMichael Tuexen error = ip6_ctloutput(so, sopt); 1782fc4d53ccSGleb Smirnoff #endif /* INET6 */ 1783fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET) 1784fc4d53ccSGleb Smirnoff else 1785fc4d53ccSGleb Smirnoff #endif 1786fc4d53ccSGleb Smirnoff #ifdef INET 1787fd7daa72SMichael Tuexen error = ip_ctloutput(so, sopt); 1788fc4d53ccSGleb Smirnoff #endif 1789fc4d53ccSGleb Smirnoff return (error); 1790fc4d53ccSGleb Smirnoff } 1791fc4d53ccSGleb Smirnoff if (((sopt->sopt_name == TCP_FUNCTION_BLK) || 1792e2833083SPeter Lei (sopt->sopt_name == TCP_FUNCTION_ALIAS))) { 1793fc4d53ccSGleb Smirnoff struct tcp_function_set fsn; 1794fc4d53ccSGleb Smirnoff 1795e2833083SPeter Lei if (sopt->sopt_name == TCP_FUNCTION_ALIAS) { 1796e2833083SPeter Lei memset(&fsn, 0, sizeof(fsn)); 1797e2833083SPeter Lei find_tcp_function_alias(tp->t_fb, &fsn); 1798e2833083SPeter Lei } else { 1799e2833083SPeter Lei strncpy(fsn.function_set_name, 1800e2833083SPeter Lei tp->t_fb->tfb_tcp_block_name, 1801c73b6f4dSEd Maste TCP_FUNCTION_NAME_LEN_MAX); 1802c73b6f4dSEd Maste fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0'; 1803e2833083SPeter Lei } 180455bceb1eSRandall Stewart fsn.pcbcnt = tp->t_fb->tfb_refcnt; 180555bceb1eSRandall Stewart INP_WUNLOCK(inp); 180655bceb1eSRandall Stewart error = sooptcopyout(sopt, &fsn, sizeof fsn); 180755bceb1eSRandall Stewart return (error); 180855bceb1eSRandall Stewart } 1809fc4d53ccSGleb Smirnoff 18103b3c08c1SMichael Tuexen /* Pass in the INP locked, callee must unlock it. */ 18113b3c08c1SMichael Tuexen return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt)); 1812fc4d53ccSGleb Smirnoff } 1813fc4d53ccSGleb Smirnoff 1814fc4d53ccSGleb Smirnoff int 1815fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1816fc4d53ccSGleb Smirnoff { 1817fc4d53ccSGleb Smirnoff struct inpcb *inp; 1818fc4d53ccSGleb Smirnoff 1819fc4d53ccSGleb Smirnoff inp = sotoinpcb(so); 1820fc4d53ccSGleb Smirnoff KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1821fc4d53ccSGleb Smirnoff 18223b3c08c1SMichael Tuexen INP_WLOCK(inp); 182353af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 18243b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 18253b3c08c1SMichael Tuexen return (ECONNRESET); 18263b3c08c1SMichael Tuexen } 1827fc4d53ccSGleb Smirnoff if (sopt->sopt_dir == SOPT_SET) 1828fc4d53ccSGleb Smirnoff return (tcp_ctloutput_set(inp, sopt)); 1829fc4d53ccSGleb Smirnoff else if (sopt->sopt_dir == SOPT_GET) 1830fc4d53ccSGleb Smirnoff return (tcp_ctloutput_get(inp, sopt)); 1831fc4d53ccSGleb Smirnoff else 1832fc4d53ccSGleb Smirnoff panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir); 183355bceb1eSRandall Stewart } 183455bceb1eSRandall Stewart 18352529f56eSJonathan T. Looney /* 18362529f56eSJonathan T. Looney * If this assert becomes untrue, we need to change the size of the buf 18372529f56eSJonathan T. Looney * variable in tcp_default_ctloutput(). 18382529f56eSJonathan T. Looney */ 18392529f56eSJonathan T. Looney #ifdef CTASSERT 18402529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN); 18412529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN); 18422529f56eSJonathan T. Looney #endif 18432529f56eSJonathan T. Looney 1844ec1db6e1SJohn Baldwin #ifdef KERN_TLS 1845ec1db6e1SJohn Baldwin static int 1846ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls) 1847ec1db6e1SJohn Baldwin { 1848ec1db6e1SJohn Baldwin struct tls_enable_v0 tls_v0; 1849ec1db6e1SJohn Baldwin int error; 1850ec1db6e1SJohn Baldwin 1851ec1db6e1SJohn Baldwin if (sopt->sopt_valsize == sizeof(tls_v0)) { 1852ec1db6e1SJohn Baldwin error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0), 1853ec1db6e1SJohn Baldwin sizeof(tls_v0)); 1854ec1db6e1SJohn Baldwin if (error) 1855ec1db6e1SJohn Baldwin return (error); 1856ec1db6e1SJohn Baldwin memset(tls, 0, sizeof(*tls)); 1857ec1db6e1SJohn Baldwin tls->cipher_key = tls_v0.cipher_key; 1858ec1db6e1SJohn Baldwin tls->iv = tls_v0.iv; 1859ec1db6e1SJohn Baldwin tls->auth_key = tls_v0.auth_key; 1860ec1db6e1SJohn Baldwin tls->cipher_algorithm = tls_v0.cipher_algorithm; 1861ec1db6e1SJohn Baldwin tls->cipher_key_len = tls_v0.cipher_key_len; 1862ec1db6e1SJohn Baldwin tls->iv_len = tls_v0.iv_len; 1863ec1db6e1SJohn Baldwin tls->auth_algorithm = tls_v0.auth_algorithm; 1864ec1db6e1SJohn Baldwin tls->auth_key_len = tls_v0.auth_key_len; 1865ec1db6e1SJohn Baldwin tls->flags = tls_v0.flags; 1866ec1db6e1SJohn Baldwin tls->tls_vmajor = tls_v0.tls_vmajor; 1867ec1db6e1SJohn Baldwin tls->tls_vminor = tls_v0.tls_vminor; 1868ec1db6e1SJohn Baldwin return (0); 1869ec1db6e1SJohn Baldwin } 1870ec1db6e1SJohn Baldwin 1871ec1db6e1SJohn Baldwin return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls))); 1872ec1db6e1SJohn Baldwin } 1873ec1db6e1SJohn Baldwin #endif 1874ec1db6e1SJohn Baldwin 1875b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo; 1876b8d60729SRandall Stewart 1877b8d60729SRandall Stewart static int 1878ea9017fbSRandall Stewart tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt) 1879b8d60729SRandall Stewart { 1880b8d60729SRandall Stewart struct cc_algo *algo; 1881b8d60729SRandall Stewart void *ptr = NULL; 18823b3c08c1SMichael Tuexen struct tcpcb *tp; 1883b8d60729SRandall Stewart struct cc_var cc_mem; 1884b8d60729SRandall Stewart char buf[TCP_CA_NAME_MAX]; 1885b8d60729SRandall Stewart size_t mem_sz; 1886b8d60729SRandall Stewart int error; 1887b8d60729SRandall Stewart 1888b8d60729SRandall Stewart INP_WUNLOCK(inp); 1889b8d60729SRandall Stewart error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1); 1890b8d60729SRandall Stewart if (error) 1891b8d60729SRandall Stewart return(error); 1892b8d60729SRandall Stewart buf[sopt->sopt_valsize] = '\0'; 1893b8d60729SRandall Stewart CC_LIST_RLOCK(); 1894ea9017fbSRandall Stewart STAILQ_FOREACH(algo, &cc_list, entries) { 1895b8d60729SRandall Stewart if (strncmp(buf, algo->name, 1896b8d60729SRandall Stewart TCP_CA_NAME_MAX) == 0) { 1897b8d60729SRandall Stewart if (algo->flags & CC_MODULE_BEING_REMOVED) { 1898b8d60729SRandall Stewart /* We can't "see" modules being unloaded */ 1899b8d60729SRandall Stewart continue; 1900b8d60729SRandall Stewart } 1901b8d60729SRandall Stewart break; 1902b8d60729SRandall Stewart } 1903ea9017fbSRandall Stewart } 1904b8d60729SRandall Stewart if (algo == NULL) { 1905b8d60729SRandall Stewart CC_LIST_RUNLOCK(); 1906b8d60729SRandall Stewart return(ESRCH); 1907b8d60729SRandall Stewart } 1908ea9017fbSRandall Stewart /* 1909ea9017fbSRandall Stewart * With a reference the algorithm cannot be removed 1910ea9017fbSRandall Stewart * so we hold a reference through the change process. 1911ea9017fbSRandall Stewart */ 1912ea9017fbSRandall Stewart cc_refer(algo); 1913ea9017fbSRandall Stewart CC_LIST_RUNLOCK(); 1914b8d60729SRandall Stewart if (algo->cb_init != NULL) { 1915b8d60729SRandall Stewart /* We can now pre-get the memory for the CC */ 1916b8d60729SRandall Stewart mem_sz = (*algo->cc_data_sz)(); 1917b8d60729SRandall Stewart if (mem_sz == 0) { 1918b8d60729SRandall Stewart goto no_mem_needed; 1919b8d60729SRandall Stewart } 1920b8d60729SRandall Stewart ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK); 1921b8d60729SRandall Stewart } else { 1922b8d60729SRandall Stewart no_mem_needed: 1923b8d60729SRandall Stewart mem_sz = 0; 1924b8d60729SRandall Stewart ptr = NULL; 1925b8d60729SRandall Stewart } 1926b8d60729SRandall Stewart /* 1927b8d60729SRandall Stewart * Make sure its all clean and zero and also get 1928b8d60729SRandall Stewart * back the inplock. 1929b8d60729SRandall Stewart */ 1930b8d60729SRandall Stewart memset(&cc_mem, 0, sizeof(cc_mem)); 1931df07bfdaSMichael Tuexen INP_WLOCK(inp); 193253af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 1933df07bfdaSMichael Tuexen INP_WUNLOCK(inp); 1934ea9017fbSRandall Stewart if (ptr) 1935df07bfdaSMichael Tuexen free(ptr, M_CC_MEM); 1936ea9017fbSRandall Stewart /* Release our temp reference */ 1937ea9017fbSRandall Stewart CC_LIST_RLOCK(); 1938ea9017fbSRandall Stewart cc_release(algo); 1939ea9017fbSRandall Stewart CC_LIST_RUNLOCK(); 1940df07bfdaSMichael Tuexen return (ECONNRESET); 1941df07bfdaSMichael Tuexen } 1942df07bfdaSMichael Tuexen tp = intotcpcb(inp); 1943df07bfdaSMichael Tuexen if (ptr != NULL) 1944b8d60729SRandall Stewart memset(ptr, 0, mem_sz); 1945b8d60729SRandall Stewart cc_mem.ccvc.tcp = tp; 1946b8d60729SRandall Stewart /* 1947b8d60729SRandall Stewart * We once again hold a write lock over the tcb so it's 1948b8d60729SRandall Stewart * safe to do these things without ordering concerns. 1949b8d60729SRandall Stewart * Note here we init into stack memory. 1950b8d60729SRandall Stewart */ 1951b8d60729SRandall Stewart if (algo->cb_init != NULL) 1952b8d60729SRandall Stewart error = algo->cb_init(&cc_mem, ptr); 1953b8d60729SRandall Stewart else 1954b8d60729SRandall Stewart error = 0; 1955b8d60729SRandall Stewart /* 1956b8d60729SRandall Stewart * The CC algorithms, when given their memory 1957b8d60729SRandall Stewart * should not fail we could in theory have a 1958b8d60729SRandall Stewart * KASSERT here. 1959b8d60729SRandall Stewart */ 1960b8d60729SRandall Stewart if (error == 0) { 1961b8d60729SRandall Stewart /* 1962b8d60729SRandall Stewart * Touchdown, lets go ahead and move the 1963b8d60729SRandall Stewart * connection to the new CC module by 1964b8d60729SRandall Stewart * copying in the cc_mem after we call 1965b8d60729SRandall Stewart * the old ones cleanup (if any). 1966b8d60729SRandall Stewart */ 1967b8d60729SRandall Stewart if (CC_ALGO(tp)->cb_destroy != NULL) 1968e68b3792SGleb Smirnoff CC_ALGO(tp)->cb_destroy(&tp->t_ccv); 1969ea9017fbSRandall Stewart /* Detach the old CC from the tcpcb */ 1970ea9017fbSRandall Stewart cc_detach(tp); 1971ea9017fbSRandall Stewart /* Copy in our temp memory that was inited */ 1972e68b3792SGleb Smirnoff memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var)); 1973ea9017fbSRandall Stewart /* Now attach the new, which takes a reference */ 1974ea9017fbSRandall Stewart cc_attach(tp, algo); 1975b8d60729SRandall Stewart /* Ok now are we where we have gotten past any conn_init? */ 1976b8d60729SRandall Stewart if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) { 1977b8d60729SRandall Stewart /* Yep run the connection init for the new CC */ 1978e68b3792SGleb Smirnoff CC_ALGO(tp)->conn_init(&tp->t_ccv); 1979b8d60729SRandall Stewart } 1980b8d60729SRandall Stewart } else if (ptr) 1981b8d60729SRandall Stewart free(ptr, M_CC_MEM); 1982b8d60729SRandall Stewart INP_WUNLOCK(inp); 1983ea9017fbSRandall Stewart /* Now lets release our temp reference */ 1984ea9017fbSRandall Stewart CC_LIST_RLOCK(); 1985ea9017fbSRandall Stewart cc_release(algo); 1986ea9017fbSRandall Stewart CC_LIST_RUNLOCK(); 1987b8d60729SRandall Stewart return (error); 1988b8d60729SRandall Stewart } 1989b8d60729SRandall Stewart 199055bceb1eSRandall Stewart int 19913b3c08c1SMichael Tuexen tcp_default_ctloutput(struct inpcb *inp, struct sockopt *sopt) 199255bceb1eSRandall Stewart { 1993fd7daa72SMichael Tuexen struct tcpcb *tp = intotcpcb(inp); 199455bceb1eSRandall Stewart int error, opt, optval; 199555bceb1eSRandall Stewart u_int ui; 199655bceb1eSRandall Stewart struct tcp_info ti; 1997b2e60773SJohn Baldwin #ifdef KERN_TLS 1998b2e60773SJohn Baldwin struct tls_enable tls; 1999528c7649SMichael Tuexen struct socket *so = inp->inp_socket; 2000b2e60773SJohn Baldwin #endif 20012529f56eSJonathan T. Looney char *pbuf, buf[TCP_LOG_ID_LEN]; 2002adc56f5aSEdward Tomasz Napierala #ifdef STATS 2003adc56f5aSEdward Tomasz Napierala struct statsblob *sbp; 2004adc56f5aSEdward Tomasz Napierala #endif 2005af6fef3aSGleb Smirnoff size_t len; 2006df8bae1dSRodney W. Grimes 2007f581a26eSGleb Smirnoff INP_WLOCK_ASSERT(inp); 200853af6903SGleb Smirnoff KASSERT((inp->inp_flags & INP_DROPPED) == 0, 2009fd7daa72SMichael Tuexen ("inp_flags == %x", inp->inp_flags)); 2010528c7649SMichael Tuexen KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL")); 2011f581a26eSGleb Smirnoff 2012f581a26eSGleb Smirnoff switch (sopt->sopt_level) { 2013f581a26eSGleb Smirnoff #ifdef INET6 2014f581a26eSGleb Smirnoff case IPPROTO_IPV6: 2015f581a26eSGleb Smirnoff MPASS(inp->inp_vflag & INP_IPV6PROTO); 2016f581a26eSGleb Smirnoff switch (sopt->sopt_name) { 2017f581a26eSGleb Smirnoff case IPV6_USE_MIN_MTU: 2018f581a26eSGleb Smirnoff tcp6_use_min_mtu(tp); 2019f581a26eSGleb Smirnoff /* FALLTHROUGH */ 2020f581a26eSGleb Smirnoff } 2021f581a26eSGleb Smirnoff INP_WUNLOCK(inp); 2022f581a26eSGleb Smirnoff return (0); 2023f581a26eSGleb Smirnoff #endif 2024f581a26eSGleb Smirnoff #ifdef INET 2025f581a26eSGleb Smirnoff case IPPROTO_IP: 2026f581a26eSGleb Smirnoff INP_WUNLOCK(inp); 2027f581a26eSGleb Smirnoff return (0); 2028f581a26eSGleb Smirnoff #endif 2029f581a26eSGleb Smirnoff } 2030f581a26eSGleb Smirnoff 2031d519cedbSGleb Smirnoff /* 2032d519cedbSGleb Smirnoff * For TCP_CCALGOOPT forward the control to CC module, for both 2033d519cedbSGleb Smirnoff * SOPT_SET and SOPT_GET. 2034d519cedbSGleb Smirnoff */ 2035d519cedbSGleb Smirnoff switch (sopt->sopt_name) { 2036d519cedbSGleb Smirnoff case TCP_CCALGOOPT: 2037d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 2038c8b53cedSMichael Tuexen if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT) 2039c8b53cedSMichael Tuexen return (EINVAL); 2040af6fef3aSGleb Smirnoff pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO); 2041af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize, 2042d519cedbSGleb Smirnoff sopt->sopt_valsize); 2043d519cedbSGleb Smirnoff if (error) { 2044af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 2045d519cedbSGleb Smirnoff return (error); 2046d519cedbSGleb Smirnoff } 2047bac5bedfSConrad Meyer INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP)); 2048d519cedbSGleb Smirnoff if (CC_ALGO(tp)->ctl_output != NULL) 2049e68b3792SGleb Smirnoff error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf); 2050d519cedbSGleb Smirnoff else 2051d519cedbSGleb Smirnoff error = ENOENT; 2052d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 2053d519cedbSGleb Smirnoff if (error == 0 && sopt->sopt_dir == SOPT_GET) 2054af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize); 2055af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 2056d519cedbSGleb Smirnoff return (error); 2057d519cedbSGleb Smirnoff } 2058d519cedbSGleb Smirnoff 2059cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 2060cfe8b629SGarrett Wollman case SOPT_SET: 2061cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2062fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 206388f6b043SBruce M Simpson case TCP_MD5SIG: 20648501a69cSRobert Watson INP_WUNLOCK(inp); 206597453e5eSClaudio Jeker if (!TCPMD5_ENABLED()) 2066fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 2067fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 20681cfd4b53SBruce M Simpson if (error) 20691e8f5ffaSRobert Watson return (error); 207097453e5eSClaudio Jeker INP_WLOCK_RECHECK(inp); 207109fe6320SNavdeep Parhar goto unlock_and_done; 2072fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 207309fe6320SNavdeep Parhar 2074df8bae1dSRodney W. Grimes case TCP_NODELAY: 2075cfe8b629SGarrett Wollman case TCP_NOOPT: 20760471a8c7SRichard Scheffenegger case TCP_LRD: 20778501a69cSRobert Watson INP_WUNLOCK(inp); 2078cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 2079cfe8b629SGarrett Wollman sizeof optval); 2080cfe8b629SGarrett Wollman if (error) 20811e8f5ffaSRobert Watson return (error); 2082cfe8b629SGarrett Wollman 20838501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 2084cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2085cfe8b629SGarrett Wollman case TCP_NODELAY: 2086cfe8b629SGarrett Wollman opt = TF_NODELAY; 2087cfe8b629SGarrett Wollman break; 2088cfe8b629SGarrett Wollman case TCP_NOOPT: 2089cfe8b629SGarrett Wollman opt = TF_NOOPT; 2090cfe8b629SGarrett Wollman break; 20910471a8c7SRichard Scheffenegger case TCP_LRD: 20920471a8c7SRichard Scheffenegger opt = TF_LRD; 20930471a8c7SRichard Scheffenegger break; 2094cfe8b629SGarrett Wollman default: 2095cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 2096cfe8b629SGarrett Wollman break; 2097cfe8b629SGarrett Wollman } 2098cfe8b629SGarrett Wollman 2099cfe8b629SGarrett Wollman if (optval) 2100cfe8b629SGarrett Wollman tp->t_flags |= opt; 2101df8bae1dSRodney W. Grimes else 2102cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 210309fe6320SNavdeep Parhar unlock_and_done: 210409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 210509fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 210609fe6320SNavdeep Parhar tcp_offload_ctloutput(tp, sopt->sopt_dir, 210709fe6320SNavdeep Parhar sopt->sopt_name); 210809fe6320SNavdeep Parhar } 210909fe6320SNavdeep Parhar #endif 21108501a69cSRobert Watson INP_WUNLOCK(inp); 2111df8bae1dSRodney W. Grimes break; 2112df8bae1dSRodney W. Grimes 2113007581c0SJonathan Lemon case TCP_NOPUSH: 21148501a69cSRobert Watson INP_WUNLOCK(inp); 2115007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 2116007581c0SJonathan Lemon sizeof optval); 2117007581c0SJonathan Lemon if (error) 21181e8f5ffaSRobert Watson return (error); 2119007581c0SJonathan Lemon 21208501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 2121007581c0SJonathan Lemon if (optval) 2122007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 2123d28b9e89SJohn Baldwin else if (tp->t_flags & TF_NOPUSH) { 2124007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 2125109eb549SGleb Smirnoff if (TCPS_HAVEESTABLISHED(tp->t_state)) { 2126109eb549SGleb Smirnoff struct epoch_tracker et; 2127109eb549SGleb Smirnoff 2128109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 2129f64dc2abSGleb Smirnoff error = tcp_output_nodrop(tp); 2130109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 2131109eb549SGleb Smirnoff } 2132007581c0SJonathan Lemon } 213309fe6320SNavdeep Parhar goto unlock_and_done; 2134007581c0SJonathan Lemon 21359e644c23SMichael Tuexen case TCP_REMOTE_UDP_ENCAPS_PORT: 21369e644c23SMichael Tuexen INP_WUNLOCK(inp); 21379e644c23SMichael Tuexen error = sooptcopyin(sopt, &optval, sizeof optval, 21389e644c23SMichael Tuexen sizeof optval); 21399e644c23SMichael Tuexen if (error) 21409e644c23SMichael Tuexen return (error); 21419e644c23SMichael Tuexen if ((optval < TCP_TUNNELING_PORT_MIN) || 21429e644c23SMichael Tuexen (optval > TCP_TUNNELING_PORT_MAX)) { 21439e644c23SMichael Tuexen /* Its got to be in range */ 21449e644c23SMichael Tuexen return (EINVAL); 21459e644c23SMichael Tuexen } 21469e644c23SMichael Tuexen if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) { 21479e644c23SMichael Tuexen /* You have to have enabled a UDP tunneling port first */ 21489e644c23SMichael Tuexen return (EINVAL); 21499e644c23SMichael Tuexen } 21509e644c23SMichael Tuexen INP_WLOCK_RECHECK(inp); 21519e644c23SMichael Tuexen if (tp->t_state != TCPS_CLOSED) { 21529e644c23SMichael Tuexen /* You can't change after you are connected */ 21539e644c23SMichael Tuexen error = EINVAL; 21549e644c23SMichael Tuexen } else { 21559e644c23SMichael Tuexen /* Ok we are all good set the port */ 21569e644c23SMichael Tuexen tp->t_port = htons(optval); 21579e644c23SMichael Tuexen } 21589e644c23SMichael Tuexen goto unlock_and_done; 21599e644c23SMichael Tuexen 2160df8bae1dSRodney W. Grimes case TCP_MAXSEG: 21618501a69cSRobert Watson INP_WUNLOCK(inp); 2162cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 2163cfe8b629SGarrett Wollman sizeof optval); 2164cfe8b629SGarrett Wollman if (error) 21651e8f5ffaSRobert Watson return (error); 2166df8bae1dSRodney W. Grimes 21678501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 216853369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 2169603724d3SBjoern A. Zeeb optval + 40 >= V_tcp_minmss) 2170cfe8b629SGarrett Wollman tp->t_maxseg = optval; 2171a0292f23SGarrett Wollman else 2172a0292f23SGarrett Wollman error = EINVAL; 217309fe6320SNavdeep Parhar goto unlock_and_done; 2174a0292f23SGarrett Wollman 2175b8af5dfaSRobert Watson case TCP_INFO: 21768501a69cSRobert Watson INP_WUNLOCK(inp); 2177b8af5dfaSRobert Watson error = EINVAL; 2178b8af5dfaSRobert Watson break; 2179b8af5dfaSRobert Watson 2180adc56f5aSEdward Tomasz Napierala case TCP_STATS: 2181adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2182adc56f5aSEdward Tomasz Napierala #ifdef STATS 2183adc56f5aSEdward Tomasz Napierala error = sooptcopyin(sopt, &optval, sizeof optval, 2184adc56f5aSEdward Tomasz Napierala sizeof optval); 2185adc56f5aSEdward Tomasz Napierala if (error) 2186adc56f5aSEdward Tomasz Napierala return (error); 2187adc56f5aSEdward Tomasz Napierala 2188adc56f5aSEdward Tomasz Napierala if (optval > 0) 2189adc56f5aSEdward Tomasz Napierala sbp = stats_blob_alloc( 2190adc56f5aSEdward Tomasz Napierala V_tcp_perconn_stats_dflt_tpl, 0); 2191adc56f5aSEdward Tomasz Napierala else 2192adc56f5aSEdward Tomasz Napierala sbp = NULL; 2193adc56f5aSEdward Tomasz Napierala 2194adc56f5aSEdward Tomasz Napierala INP_WLOCK_RECHECK(inp); 2195adc56f5aSEdward Tomasz Napierala if ((tp->t_stats != NULL && sbp == NULL) || 2196adc56f5aSEdward Tomasz Napierala (tp->t_stats == NULL && sbp != NULL)) { 2197adc56f5aSEdward Tomasz Napierala struct statsblob *t = tp->t_stats; 2198adc56f5aSEdward Tomasz Napierala tp->t_stats = sbp; 2199adc56f5aSEdward Tomasz Napierala sbp = t; 2200adc56f5aSEdward Tomasz Napierala } 2201adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2202adc56f5aSEdward Tomasz Napierala 2203adc56f5aSEdward Tomasz Napierala stats_blob_destroy(sbp); 2204adc56f5aSEdward Tomasz Napierala #else 2205adc56f5aSEdward Tomasz Napierala return (EOPNOTSUPP); 2206adc56f5aSEdward Tomasz Napierala #endif /* !STATS */ 2207adc56f5aSEdward Tomasz Napierala break; 2208adc56f5aSEdward Tomasz Napierala 2209dbc42409SLawrence Stewart case TCP_CONGESTION: 2210ea9017fbSRandall Stewart error = tcp_set_cc_mod(inp, sopt); 221173e263b1SGleb Smirnoff break; 2212dbc42409SLawrence Stewart 2213a034518aSAndrew Gallatin case TCP_REUSPORT_LB_NUMA: 2214a034518aSAndrew Gallatin INP_WUNLOCK(inp); 2215a034518aSAndrew Gallatin error = sooptcopyin(sopt, &optval, sizeof(optval), 2216a034518aSAndrew Gallatin sizeof(optval)); 2217a034518aSAndrew Gallatin INP_WLOCK_RECHECK(inp); 2218a034518aSAndrew Gallatin if (!error) 2219a034518aSAndrew Gallatin error = in_pcblbgroup_numa(inp, optval); 2220a034518aSAndrew Gallatin INP_WUNLOCK(inp); 2221a034518aSAndrew Gallatin break; 2222a034518aSAndrew Gallatin 2223b2e60773SJohn Baldwin #ifdef KERN_TLS 2224b2e60773SJohn Baldwin case TCP_TXTLS_ENABLE: 2225b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2226ec1db6e1SJohn Baldwin error = copyin_tls_enable(sopt, &tls); 2227b2e60773SJohn Baldwin if (error) 2228b2e60773SJohn Baldwin break; 2229fd7daa72SMichael Tuexen error = ktls_enable_tx(so, &tls); 2230b2e60773SJohn Baldwin break; 2231b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2232b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2233b2e60773SJohn Baldwin error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 2234b2e60773SJohn Baldwin if (error) 2235b2e60773SJohn Baldwin return (error); 2236b2e60773SJohn Baldwin 2237b2e60773SJohn Baldwin INP_WLOCK_RECHECK(inp); 2238fd7daa72SMichael Tuexen error = ktls_set_tx_mode(so, ui); 2239b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2240b2e60773SJohn Baldwin break; 2241f1f93475SJohn Baldwin case TCP_RXTLS_ENABLE: 2242f1f93475SJohn Baldwin INP_WUNLOCK(inp); 2243f1f93475SJohn Baldwin error = sooptcopyin(sopt, &tls, sizeof(tls), 2244f1f93475SJohn Baldwin sizeof(tls)); 2245f1f93475SJohn Baldwin if (error) 2246f1f93475SJohn Baldwin break; 2247fd7daa72SMichael Tuexen error = ktls_enable_rx(so, &tls); 2248f1f93475SJohn Baldwin break; 2249b2e60773SJohn Baldwin #endif 225008af8aacSRandall Stewart case TCP_MAXUNACKTIME: 22519077f387SGleb Smirnoff case TCP_KEEPIDLE: 22529077f387SGleb Smirnoff case TCP_KEEPINTVL: 22539077f387SGleb Smirnoff case TCP_KEEPINIT: 22549077f387SGleb Smirnoff INP_WUNLOCK(inp); 22559077f387SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 22569077f387SGleb Smirnoff if (error) 22579077f387SGleb Smirnoff return (error); 22589077f387SGleb Smirnoff 22599077f387SGleb Smirnoff if (ui > (UINT_MAX / hz)) { 22609077f387SGleb Smirnoff error = EINVAL; 22619077f387SGleb Smirnoff break; 22629077f387SGleb Smirnoff } 22639077f387SGleb Smirnoff ui *= hz; 22649077f387SGleb Smirnoff 22659077f387SGleb Smirnoff INP_WLOCK_RECHECK(inp); 22669077f387SGleb Smirnoff switch (sopt->sopt_name) { 226708af8aacSRandall Stewart case TCP_MAXUNACKTIME: 226808af8aacSRandall Stewart tp->t_maxunacktime = ui; 226908af8aacSRandall Stewart break; 227008af8aacSRandall Stewart 22719077f387SGleb Smirnoff case TCP_KEEPIDLE: 22729077f387SGleb Smirnoff tp->t_keepidle = ui; 22739077f387SGleb Smirnoff /* 22749077f387SGleb Smirnoff * XXX: better check current remaining 22759077f387SGleb Smirnoff * timeout and "merge" it with new value. 22769077f387SGleb Smirnoff */ 22779077f387SGleb Smirnoff if ((tp->t_state > TCPS_LISTEN) && 22789077f387SGleb Smirnoff (tp->t_state <= TCPS_CLOSING)) 22799077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 22809077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 22819077f387SGleb Smirnoff break; 22829077f387SGleb Smirnoff case TCP_KEEPINTVL: 22839077f387SGleb Smirnoff tp->t_keepintvl = ui; 22849077f387SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 22859077f387SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 22869077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 22879077f387SGleb Smirnoff TP_MAXIDLE(tp)); 22889077f387SGleb Smirnoff break; 22899077f387SGleb Smirnoff case TCP_KEEPINIT: 22909077f387SGleb Smirnoff tp->t_keepinit = ui; 22919077f387SGleb Smirnoff if (tp->t_state == TCPS_SYN_RECEIVED || 22929077f387SGleb Smirnoff tp->t_state == TCPS_SYN_SENT) 22939077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 22949077f387SGleb Smirnoff TP_KEEPINIT(tp)); 22959077f387SGleb Smirnoff break; 22969077f387SGleb Smirnoff } 229709fe6320SNavdeep Parhar goto unlock_and_done; 22989077f387SGleb Smirnoff 229985c05144SGleb Smirnoff case TCP_KEEPCNT: 230085c05144SGleb Smirnoff INP_WUNLOCK(inp); 230185c05144SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 230285c05144SGleb Smirnoff if (error) 230385c05144SGleb Smirnoff return (error); 230485c05144SGleb Smirnoff 230585c05144SGleb Smirnoff INP_WLOCK_RECHECK(inp); 230685c05144SGleb Smirnoff tp->t_keepcnt = ui; 230785c05144SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 230885c05144SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 230985c05144SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 231085c05144SGleb Smirnoff TP_MAXIDLE(tp)); 231185c05144SGleb Smirnoff goto unlock_and_done; 231285c05144SGleb Smirnoff 231386a996e6SHiren Panchasara #ifdef TCPPCAP 231486a996e6SHiren Panchasara case TCP_PCAP_OUT: 231586a996e6SHiren Panchasara case TCP_PCAP_IN: 231686a996e6SHiren Panchasara INP_WUNLOCK(inp); 231786a996e6SHiren Panchasara error = sooptcopyin(sopt, &optval, sizeof optval, 231886a996e6SHiren Panchasara sizeof optval); 231986a996e6SHiren Panchasara if (error) 232086a996e6SHiren Panchasara return (error); 232186a996e6SHiren Panchasara 232286a996e6SHiren Panchasara INP_WLOCK_RECHECK(inp); 232386a996e6SHiren Panchasara if (optval >= 0) 232486a996e6SHiren Panchasara tcp_pcap_set_sock_max(TCP_PCAP_OUT ? 232586a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts), 232686a996e6SHiren Panchasara optval); 232786a996e6SHiren Panchasara else 232886a996e6SHiren Panchasara error = EINVAL; 232986a996e6SHiren Panchasara goto unlock_and_done; 233086a996e6SHiren Panchasara #endif 233186a996e6SHiren Panchasara 2332c560df6fSPatrick Kelsey case TCP_FASTOPEN: { 2333c560df6fSPatrick Kelsey struct tcp_fastopen tfo_optval; 2334c560df6fSPatrick Kelsey 2335281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2336c560df6fSPatrick Kelsey if (!V_tcp_fastopen_client_enable && 2337c560df6fSPatrick Kelsey !V_tcp_fastopen_server_enable) 2338281a0fd4SPatrick Kelsey return (EPERM); 2339281a0fd4SPatrick Kelsey 2340c560df6fSPatrick Kelsey error = sooptcopyin(sopt, &tfo_optval, 2341c560df6fSPatrick Kelsey sizeof(tfo_optval), sizeof(int)); 2342281a0fd4SPatrick Kelsey if (error) 2343281a0fd4SPatrick Kelsey return (error); 2344281a0fd4SPatrick Kelsey 2345281a0fd4SPatrick Kelsey INP_WLOCK_RECHECK(inp); 2346d442a657SMichael Tuexen if ((tp->t_state != TCPS_CLOSED) && 2347d442a657SMichael Tuexen (tp->t_state != TCPS_LISTEN)) { 2348d442a657SMichael Tuexen error = EINVAL; 2349d442a657SMichael Tuexen goto unlock_and_done; 2350d442a657SMichael Tuexen } 2351c560df6fSPatrick Kelsey if (tfo_optval.enable) { 2352c560df6fSPatrick Kelsey if (tp->t_state == TCPS_LISTEN) { 2353c560df6fSPatrick Kelsey if (!V_tcp_fastopen_server_enable) { 2354c560df6fSPatrick Kelsey error = EPERM; 2355c560df6fSPatrick Kelsey goto unlock_and_done; 2356c560df6fSPatrick Kelsey } 2357c560df6fSPatrick Kelsey 2358c560df6fSPatrick Kelsey if (tp->t_tfo_pending == NULL) 2359281a0fd4SPatrick Kelsey tp->t_tfo_pending = 2360281a0fd4SPatrick Kelsey tcp_fastopen_alloc_counter(); 2361c560df6fSPatrick Kelsey } else { 2362c560df6fSPatrick Kelsey /* 2363c560df6fSPatrick Kelsey * If a pre-shared key was provided, 2364c560df6fSPatrick Kelsey * stash it in the client cookie 2365c560df6fSPatrick Kelsey * field of the tcpcb for use during 2366c560df6fSPatrick Kelsey * connect. 2367c560df6fSPatrick Kelsey */ 2368c560df6fSPatrick Kelsey if (sopt->sopt_valsize == 2369c560df6fSPatrick Kelsey sizeof(tfo_optval)) { 2370c560df6fSPatrick Kelsey memcpy(tp->t_tfo_cookie.client, 2371c560df6fSPatrick Kelsey tfo_optval.psk, 2372c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN); 2373c560df6fSPatrick Kelsey tp->t_tfo_client_cookie_len = 2374c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN; 2375c560df6fSPatrick Kelsey } 2376c560df6fSPatrick Kelsey } 2377d442a657SMichael Tuexen tp->t_flags |= TF_FASTOPEN; 2378281a0fd4SPatrick Kelsey } else 2379281a0fd4SPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 2380281a0fd4SPatrick Kelsey goto unlock_and_done; 2381c560df6fSPatrick Kelsey } 2382281a0fd4SPatrick Kelsey 2383e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 23842529f56eSJonathan T. Looney case TCP_LOG: 23852529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23862529f56eSJonathan T. Looney error = sooptcopyin(sopt, &optval, sizeof optval, 23872529f56eSJonathan T. Looney sizeof optval); 23882529f56eSJonathan T. Looney if (error) 23892529f56eSJonathan T. Looney return (error); 23902529f56eSJonathan T. Looney 23912529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 23922529f56eSJonathan T. Looney error = tcp_log_state_change(tp, optval); 23932529f56eSJonathan T. Looney goto unlock_and_done; 23942529f56eSJonathan T. Looney 23952529f56eSJonathan T. Looney case TCP_LOGBUF: 23962529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23972529f56eSJonathan T. Looney error = EINVAL; 23982529f56eSJonathan T. Looney break; 23992529f56eSJonathan T. Looney 24002529f56eSJonathan T. Looney case TCP_LOGID: 24012529f56eSJonathan T. Looney INP_WUNLOCK(inp); 24022529f56eSJonathan T. Looney error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0); 24032529f56eSJonathan T. Looney if (error) 24042529f56eSJonathan T. Looney break; 24052529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 24062529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 24072529f56eSJonathan T. Looney error = tcp_log_set_id(tp, buf); 24082529f56eSJonathan T. Looney /* tcp_log_set_id() unlocks the INP. */ 24092529f56eSJonathan T. Looney break; 24102529f56eSJonathan T. Looney 24112529f56eSJonathan T. Looney case TCP_LOGDUMP: 24122529f56eSJonathan T. Looney case TCP_LOGDUMPID: 24132529f56eSJonathan T. Looney INP_WUNLOCK(inp); 24142529f56eSJonathan T. Looney error = 24152529f56eSJonathan T. Looney sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0); 24162529f56eSJonathan T. Looney if (error) 24172529f56eSJonathan T. Looney break; 24182529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 24192529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 24202529f56eSJonathan T. Looney if (sopt->sopt_name == TCP_LOGDUMP) { 24212529f56eSJonathan T. Looney error = tcp_log_dump_tp_logbuf(tp, buf, 24222529f56eSJonathan T. Looney M_WAITOK, true); 24232529f56eSJonathan T. Looney INP_WUNLOCK(inp); 24242529f56eSJonathan T. Looney } else { 24252529f56eSJonathan T. Looney tcp_log_dump_tp_bucket_logbufs(tp, buf); 24262529f56eSJonathan T. Looney /* 24272529f56eSJonathan T. Looney * tcp_log_dump_tp_bucket_logbufs() drops the 24282529f56eSJonathan T. Looney * INP lock. 24292529f56eSJonathan T. Looney */ 24302529f56eSJonathan T. Looney } 24312529f56eSJonathan T. Looney break; 2432e24e5683SJonathan T. Looney #endif 24332529f56eSJonathan T. Looney 2434df8bae1dSRodney W. Grimes default: 24358501a69cSRobert Watson INP_WUNLOCK(inp); 2436df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2437df8bae1dSRodney W. Grimes break; 2438df8bae1dSRodney W. Grimes } 2439df8bae1dSRodney W. Grimes break; 2440df8bae1dSRodney W. Grimes 2441cfe8b629SGarrett Wollman case SOPT_GET: 24421e8f5ffaSRobert Watson tp = intotcpcb(inp); 2443cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2444fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 244588f6b043SBruce M Simpson case TCP_MD5SIG: 24468501a69cSRobert Watson INP_WUNLOCK(inp); 244797453e5eSClaudio Jeker if (!TCPMD5_ENABLED()) 2448fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 2449fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 24501cfd4b53SBruce M Simpson break; 2451265ed012SBruce M Simpson #endif 24521e8f5ffaSRobert Watson 2453df8bae1dSRodney W. Grimes case TCP_NODELAY: 2454cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 24558501a69cSRobert Watson INP_WUNLOCK(inp); 2456b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2457df8bae1dSRodney W. Grimes break; 2458df8bae1dSRodney W. Grimes case TCP_MAXSEG: 2459cfe8b629SGarrett Wollman optval = tp->t_maxseg; 24608501a69cSRobert Watson INP_WUNLOCK(inp); 2461b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2462df8bae1dSRodney W. Grimes break; 24639e644c23SMichael Tuexen case TCP_REMOTE_UDP_ENCAPS_PORT: 24649e644c23SMichael Tuexen optval = ntohs(tp->t_port); 24659e644c23SMichael Tuexen INP_WUNLOCK(inp); 24669e644c23SMichael Tuexen error = sooptcopyout(sopt, &optval, sizeof optval); 24679e644c23SMichael Tuexen break; 2468a0292f23SGarrett Wollman case TCP_NOOPT: 2469cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 24708501a69cSRobert Watson INP_WUNLOCK(inp); 2471b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2472a0292f23SGarrett Wollman break; 2473a0292f23SGarrett Wollman case TCP_NOPUSH: 2474cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 24758501a69cSRobert Watson INP_WUNLOCK(inp); 2476b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2477b8af5dfaSRobert Watson break; 2478b8af5dfaSRobert Watson case TCP_INFO: 2479b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 24808501a69cSRobert Watson INP_WUNLOCK(inp); 2481b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 2482a0292f23SGarrett Wollman break; 2483adc56f5aSEdward Tomasz Napierala case TCP_STATS: 2484adc56f5aSEdward Tomasz Napierala { 2485adc56f5aSEdward Tomasz Napierala #ifdef STATS 2486adc56f5aSEdward Tomasz Napierala int nheld; 2487adc56f5aSEdward Tomasz Napierala TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0; 2488adc56f5aSEdward Tomasz Napierala 2489adc56f5aSEdward Tomasz Napierala error = 0; 2490adc56f5aSEdward Tomasz Napierala socklen_t outsbsz = sopt->sopt_valsize; 2491adc56f5aSEdward Tomasz Napierala if (tp->t_stats == NULL) 2492adc56f5aSEdward Tomasz Napierala error = ENOENT; 2493adc56f5aSEdward Tomasz Napierala else if (outsbsz >= tp->t_stats->cursz) 2494adc56f5aSEdward Tomasz Napierala outsbsz = tp->t_stats->cursz; 2495adc56f5aSEdward Tomasz Napierala else if (outsbsz >= sizeof(struct statsblob)) 2496adc56f5aSEdward Tomasz Napierala outsbsz = sizeof(struct statsblob); 2497adc56f5aSEdward Tomasz Napierala else 2498adc56f5aSEdward Tomasz Napierala error = EINVAL; 2499adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2500adc56f5aSEdward Tomasz Napierala if (error) 2501adc56f5aSEdward Tomasz Napierala break; 2502adc56f5aSEdward Tomasz Napierala 2503adc56f5aSEdward Tomasz Napierala sbp = sopt->sopt_val; 2504adc56f5aSEdward Tomasz Napierala nheld = atop(round_page(((vm_offset_t)sbp) + 2505adc56f5aSEdward Tomasz Napierala (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp)); 2506adc56f5aSEdward Tomasz Napierala vm_page_t ma[nheld]; 2507adc56f5aSEdward Tomasz Napierala if (vm_fault_quick_hold_pages( 2508adc56f5aSEdward Tomasz Napierala &curproc->p_vmspace->vm_map, (vm_offset_t)sbp, 2509adc56f5aSEdward Tomasz Napierala outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma, 2510adc56f5aSEdward Tomasz Napierala nheld) < 0) { 2511adc56f5aSEdward Tomasz Napierala error = EFAULT; 2512adc56f5aSEdward Tomasz Napierala break; 2513adc56f5aSEdward Tomasz Napierala } 2514adc56f5aSEdward Tomasz Napierala 2515adc56f5aSEdward Tomasz Napierala if ((error = copyin_nofault(&(sbp->flags), &sbflags, 2516adc56f5aSEdward Tomasz Napierala SIZEOF_MEMBER(struct statsblob, flags)))) 2517adc56f5aSEdward Tomasz Napierala goto unhold; 2518adc56f5aSEdward Tomasz Napierala 2519adc56f5aSEdward Tomasz Napierala INP_WLOCK_RECHECK(inp); 2520adc56f5aSEdward Tomasz Napierala error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats, 2521adc56f5aSEdward Tomasz Napierala sbflags | SB_CLONE_USRDSTNOFAULT); 2522adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2523adc56f5aSEdward Tomasz Napierala sopt->sopt_valsize = outsbsz; 2524adc56f5aSEdward Tomasz Napierala unhold: 2525adc56f5aSEdward Tomasz Napierala vm_page_unhold_pages(ma, nheld); 2526adc56f5aSEdward Tomasz Napierala #else 2527adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2528adc56f5aSEdward Tomasz Napierala error = EOPNOTSUPP; 2529adc56f5aSEdward Tomasz Napierala #endif /* !STATS */ 2530adc56f5aSEdward Tomasz Napierala break; 2531adc56f5aSEdward Tomasz Napierala } 2532dbc42409SLawrence Stewart case TCP_CONGESTION: 2533af6fef3aSGleb Smirnoff len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 2534dbc42409SLawrence Stewart INP_WUNLOCK(inp); 2535af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, buf, len + 1); 2536dbc42409SLawrence Stewart break; 253708af8aacSRandall Stewart case TCP_MAXUNACKTIME: 25382f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 25392f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 25402f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 25412f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 25422f3eb7f4SGleb Smirnoff switch (sopt->sopt_name) { 254308af8aacSRandall Stewart case TCP_MAXUNACKTIME: 254408af8aacSRandall Stewart ui = TP_MAXUNACKTIME(tp) / hz; 254508af8aacSRandall Stewart break; 25462f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 25475a17b6adSMichael Tuexen ui = TP_KEEPIDLE(tp) / hz; 25482f3eb7f4SGleb Smirnoff break; 25492f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 25505a17b6adSMichael Tuexen ui = TP_KEEPINTVL(tp) / hz; 25512f3eb7f4SGleb Smirnoff break; 25522f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 25535a17b6adSMichael Tuexen ui = TP_KEEPINIT(tp) / hz; 25542f3eb7f4SGleb Smirnoff break; 25552f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 25565a17b6adSMichael Tuexen ui = TP_KEEPCNT(tp); 25572f3eb7f4SGleb Smirnoff break; 25582f3eb7f4SGleb Smirnoff } 25592f3eb7f4SGleb Smirnoff INP_WUNLOCK(inp); 25602f3eb7f4SGleb Smirnoff error = sooptcopyout(sopt, &ui, sizeof(ui)); 25612f3eb7f4SGleb Smirnoff break; 256286a996e6SHiren Panchasara #ifdef TCPPCAP 256386a996e6SHiren Panchasara case TCP_PCAP_OUT: 256486a996e6SHiren Panchasara case TCP_PCAP_IN: 256586a996e6SHiren Panchasara optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ? 256686a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts)); 256786a996e6SHiren Panchasara INP_WUNLOCK(inp); 256886a996e6SHiren Panchasara error = sooptcopyout(sopt, &optval, sizeof optval); 256986a996e6SHiren Panchasara break; 257086a996e6SHiren Panchasara #endif 2571281a0fd4SPatrick Kelsey case TCP_FASTOPEN: 2572281a0fd4SPatrick Kelsey optval = tp->t_flags & TF_FASTOPEN; 2573281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2574281a0fd4SPatrick Kelsey error = sooptcopyout(sopt, &optval, sizeof optval); 2575281a0fd4SPatrick Kelsey break; 2576e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 25772529f56eSJonathan T. Looney case TCP_LOG: 25782529f56eSJonathan T. Looney optval = tp->t_logstate; 25792529f56eSJonathan T. Looney INP_WUNLOCK(inp); 25802529f56eSJonathan T. Looney error = sooptcopyout(sopt, &optval, sizeof(optval)); 25812529f56eSJonathan T. Looney break; 25822529f56eSJonathan T. Looney case TCP_LOGBUF: 25832529f56eSJonathan T. Looney /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */ 25842529f56eSJonathan T. Looney error = tcp_log_getlogbuf(sopt, tp); 25852529f56eSJonathan T. Looney break; 25862529f56eSJonathan T. Looney case TCP_LOGID: 25872529f56eSJonathan T. Looney len = tcp_log_get_id(tp, buf); 25882529f56eSJonathan T. Looney INP_WUNLOCK(inp); 25892529f56eSJonathan T. Looney error = sooptcopyout(sopt, buf, len + 1); 25902529f56eSJonathan T. Looney break; 25912529f56eSJonathan T. Looney case TCP_LOGDUMP: 25922529f56eSJonathan T. Looney case TCP_LOGDUMPID: 25932529f56eSJonathan T. Looney INP_WUNLOCK(inp); 25942529f56eSJonathan T. Looney error = EINVAL; 25952529f56eSJonathan T. Looney break; 2596e24e5683SJonathan T. Looney #endif 2597b2e60773SJohn Baldwin #ifdef KERN_TLS 2598b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2599fd7daa72SMichael Tuexen error = ktls_get_tx_mode(so, &optval); 2600b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2601bf256782SMark Johnston if (error == 0) 2602bf256782SMark Johnston error = sooptcopyout(sopt, &optval, 2603bf256782SMark Johnston sizeof(optval)); 2604b2e60773SJohn Baldwin break; 2605f1f93475SJohn Baldwin case TCP_RXTLS_MODE: 2606fd7daa72SMichael Tuexen error = ktls_get_rx_mode(so, &optval); 2607f1f93475SJohn Baldwin INP_WUNLOCK(inp); 2608bf256782SMark Johnston if (error == 0) 2609bf256782SMark Johnston error = sooptcopyout(sopt, &optval, 2610bf256782SMark Johnston sizeof(optval)); 2611f1f93475SJohn Baldwin break; 2612b2e60773SJohn Baldwin #endif 26130471a8c7SRichard Scheffenegger case TCP_LRD: 26140471a8c7SRichard Scheffenegger optval = tp->t_flags & TF_LRD; 26150471a8c7SRichard Scheffenegger INP_WUNLOCK(inp); 26160471a8c7SRichard Scheffenegger error = sooptcopyout(sopt, &optval, sizeof optval); 26170471a8c7SRichard Scheffenegger break; 2618df8bae1dSRodney W. Grimes default: 26198501a69cSRobert Watson INP_WUNLOCK(inp); 2620df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2621df8bae1dSRodney W. Grimes break; 2622df8bae1dSRodney W. Grimes } 2623df8bae1dSRodney W. Grimes break; 2624df8bae1dSRodney W. Grimes } 2625df8bae1dSRodney W. Grimes return (error); 2626df8bae1dSRodney W. Grimes } 26278501a69cSRobert Watson #undef INP_WLOCK_RECHECK 2628bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP 2629df8bae1dSRodney W. Grimes 263026e30fbbSDavid Greenman /* 2631df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 2632df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 2633df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 2634df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 2635df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 2636df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 2637df8bae1dSRodney W. Grimes */ 2638623dce13SRobert Watson static void 2639ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp) 2640df8bae1dSRodney W. Grimes { 26419eb0e832SGleb Smirnoff struct inpcb *inp = tptoinpcb(tp); 26429eb0e832SGleb Smirnoff struct socket *so = tptosocket(tp); 2643e6e0b5ffSRobert Watson 264497a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 26458501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2646df8bae1dSRodney W. Grimes 2647623dce13SRobert Watson /* 2648623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 2649623dce13SRobert Watson * socket is still open. 2650623dce13SRobert Watson */ 26518db239dcSMichael Tuexen if (tp->t_state < TCPS_ESTABLISHED && 26528db239dcSMichael Tuexen !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) { 2653df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2654623dce13SRobert Watson KASSERT(tp != NULL, 2655623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 2656623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 2657243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 2658623dce13SRobert Watson KASSERT(tp != NULL, 2659623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 2660623dce13SRobert Watson } else { 2661df8bae1dSRodney W. Grimes soisdisconnecting(so); 2662df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 2663623dce13SRobert Watson tcp_usrclosed(tp); 2664ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 2665f64dc2abSGleb Smirnoff /* Ignore stack's drop request, we already at it. */ 2666f64dc2abSGleb Smirnoff (void)tcp_output_nodrop(tp); 2667df8bae1dSRodney W. Grimes } 2668df8bae1dSRodney W. Grimes } 2669df8bae1dSRodney W. Grimes 2670df8bae1dSRodney W. Grimes /* 2671df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 2672df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 2673df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 2674df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 2675df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 2676df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 2677df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 2678df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 2679df8bae1dSRodney W. Grimes */ 2680623dce13SRobert Watson static void 2681ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp) 2682df8bae1dSRodney W. Grimes { 2683df8bae1dSRodney W. Grimes 268497a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 26859eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 2686e6e0b5ffSRobert Watson 2687df8bae1dSRodney W. Grimes switch (tp->t_state) { 2688df8bae1dSRodney W. Grimes case TCPS_LISTEN: 268909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 269009fe6320SNavdeep Parhar tcp_offload_listen_stop(tp); 269109fe6320SNavdeep Parhar #endif 2692550e9d42SHiren Panchasara tcp_state_change(tp, TCPS_CLOSED); 2693bc65987aSKip Macy /* FALLTHROUGH */ 2694bc65987aSKip Macy case TCPS_CLOSED: 2695df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2696623dce13SRobert Watson /* 2697623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 2698623dce13SRobert Watson * still open. 2699623dce13SRobert Watson */ 2700623dce13SRobert Watson KASSERT(tp != NULL, 2701623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 2702df8bae1dSRodney W. Grimes break; 2703df8bae1dSRodney W. Grimes 2704a0292f23SGarrett Wollman case TCPS_SYN_SENT: 2705df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2706a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 2707a0292f23SGarrett Wollman break; 2708a0292f23SGarrett Wollman 2709df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 271057f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2711df8bae1dSRodney W. Grimes break; 2712df8bae1dSRodney W. Grimes 2713df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 271457f60867SMark Johnston tcp_state_change(tp, TCPS_LAST_ACK); 2715df8bae1dSRodney W. Grimes break; 2716df8bae1dSRodney W. Grimes } 271708af8aacSRandall Stewart if (tp->t_acktime == 0) 271808af8aacSRandall Stewart tp->t_acktime = ticks; 2719abc7d910SRobert Watson if (tp->t_state >= TCPS_FIN_WAIT_2) { 27209eb0e832SGleb Smirnoff soisdisconnected(tptosocket(tp)); 2721abc7d910SRobert Watson /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 27227c72af87SMohan Srinivasan if (tp->t_state == TCPS_FIN_WAIT_2) { 27237c72af87SMohan Srinivasan int timeout; 27247c72af87SMohan Srinivasan 27257c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 27269077f387SGleb Smirnoff tcp_finwait2_timeout : TP_MAXIDLE(tp); 2727b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 2728b6239c4aSAndras Olah } 2729df8bae1dSRodney W. Grimes } 27307c72af87SMohan Srinivasan } 2731497057eeSRobert Watson 2732497057eeSRobert Watson #ifdef DDB 2733497057eeSRobert Watson static void 2734497057eeSRobert Watson db_print_indent(int indent) 2735497057eeSRobert Watson { 2736497057eeSRobert Watson int i; 2737497057eeSRobert Watson 2738497057eeSRobert Watson for (i = 0; i < indent; i++) 2739497057eeSRobert Watson db_printf(" "); 2740497057eeSRobert Watson } 2741497057eeSRobert Watson 2742497057eeSRobert Watson static void 2743497057eeSRobert Watson db_print_tstate(int t_state) 2744497057eeSRobert Watson { 2745497057eeSRobert Watson 2746497057eeSRobert Watson switch (t_state) { 2747497057eeSRobert Watson case TCPS_CLOSED: 2748497057eeSRobert Watson db_printf("TCPS_CLOSED"); 2749497057eeSRobert Watson return; 2750497057eeSRobert Watson 2751497057eeSRobert Watson case TCPS_LISTEN: 2752497057eeSRobert Watson db_printf("TCPS_LISTEN"); 2753497057eeSRobert Watson return; 2754497057eeSRobert Watson 2755497057eeSRobert Watson case TCPS_SYN_SENT: 2756497057eeSRobert Watson db_printf("TCPS_SYN_SENT"); 2757497057eeSRobert Watson return; 2758497057eeSRobert Watson 2759497057eeSRobert Watson case TCPS_SYN_RECEIVED: 2760497057eeSRobert Watson db_printf("TCPS_SYN_RECEIVED"); 2761497057eeSRobert Watson return; 2762497057eeSRobert Watson 2763497057eeSRobert Watson case TCPS_ESTABLISHED: 2764497057eeSRobert Watson db_printf("TCPS_ESTABLISHED"); 2765497057eeSRobert Watson return; 2766497057eeSRobert Watson 2767497057eeSRobert Watson case TCPS_CLOSE_WAIT: 2768497057eeSRobert Watson db_printf("TCPS_CLOSE_WAIT"); 2769497057eeSRobert Watson return; 2770497057eeSRobert Watson 2771497057eeSRobert Watson case TCPS_FIN_WAIT_1: 2772497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_1"); 2773497057eeSRobert Watson return; 2774497057eeSRobert Watson 2775497057eeSRobert Watson case TCPS_CLOSING: 2776497057eeSRobert Watson db_printf("TCPS_CLOSING"); 2777497057eeSRobert Watson return; 2778497057eeSRobert Watson 2779497057eeSRobert Watson case TCPS_LAST_ACK: 2780497057eeSRobert Watson db_printf("TCPS_LAST_ACK"); 2781497057eeSRobert Watson return; 2782497057eeSRobert Watson 2783497057eeSRobert Watson case TCPS_FIN_WAIT_2: 2784497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_2"); 2785497057eeSRobert Watson return; 2786497057eeSRobert Watson 2787497057eeSRobert Watson case TCPS_TIME_WAIT: 2788497057eeSRobert Watson db_printf("TCPS_TIME_WAIT"); 2789497057eeSRobert Watson return; 2790497057eeSRobert Watson 2791497057eeSRobert Watson default: 2792497057eeSRobert Watson db_printf("unknown"); 2793497057eeSRobert Watson return; 2794497057eeSRobert Watson } 2795497057eeSRobert Watson } 2796497057eeSRobert Watson 2797497057eeSRobert Watson static void 2798497057eeSRobert Watson db_print_tflags(u_int t_flags) 2799497057eeSRobert Watson { 2800497057eeSRobert Watson int comma; 2801497057eeSRobert Watson 2802497057eeSRobert Watson comma = 0; 2803497057eeSRobert Watson if (t_flags & TF_ACKNOW) { 2804497057eeSRobert Watson db_printf("%sTF_ACKNOW", comma ? ", " : ""); 2805497057eeSRobert Watson comma = 1; 2806497057eeSRobert Watson } 2807497057eeSRobert Watson if (t_flags & TF_DELACK) { 2808497057eeSRobert Watson db_printf("%sTF_DELACK", comma ? ", " : ""); 2809497057eeSRobert Watson comma = 1; 2810497057eeSRobert Watson } 2811497057eeSRobert Watson if (t_flags & TF_NODELAY) { 2812497057eeSRobert Watson db_printf("%sTF_NODELAY", comma ? ", " : ""); 2813497057eeSRobert Watson comma = 1; 2814497057eeSRobert Watson } 2815497057eeSRobert Watson if (t_flags & TF_NOOPT) { 2816497057eeSRobert Watson db_printf("%sTF_NOOPT", comma ? ", " : ""); 2817497057eeSRobert Watson comma = 1; 2818497057eeSRobert Watson } 2819497057eeSRobert Watson if (t_flags & TF_SENTFIN) { 2820497057eeSRobert Watson db_printf("%sTF_SENTFIN", comma ? ", " : ""); 2821497057eeSRobert Watson comma = 1; 2822497057eeSRobert Watson } 2823497057eeSRobert Watson if (t_flags & TF_REQ_SCALE) { 2824497057eeSRobert Watson db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 2825497057eeSRobert Watson comma = 1; 2826497057eeSRobert Watson } 2827497057eeSRobert Watson if (t_flags & TF_RCVD_SCALE) { 2828497057eeSRobert Watson db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 2829497057eeSRobert Watson comma = 1; 2830497057eeSRobert Watson } 2831497057eeSRobert Watson if (t_flags & TF_REQ_TSTMP) { 2832497057eeSRobert Watson db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 2833497057eeSRobert Watson comma = 1; 2834497057eeSRobert Watson } 2835497057eeSRobert Watson if (t_flags & TF_RCVD_TSTMP) { 2836497057eeSRobert Watson db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 2837497057eeSRobert Watson comma = 1; 2838497057eeSRobert Watson } 2839497057eeSRobert Watson if (t_flags & TF_SACK_PERMIT) { 2840497057eeSRobert Watson db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 2841497057eeSRobert Watson comma = 1; 2842497057eeSRobert Watson } 2843497057eeSRobert Watson if (t_flags & TF_NEEDSYN) { 2844497057eeSRobert Watson db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 2845497057eeSRobert Watson comma = 1; 2846497057eeSRobert Watson } 2847497057eeSRobert Watson if (t_flags & TF_NEEDFIN) { 2848497057eeSRobert Watson db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 2849497057eeSRobert Watson comma = 1; 2850497057eeSRobert Watson } 2851497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 2852497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 2853497057eeSRobert Watson comma = 1; 2854497057eeSRobert Watson } 28553f169c54SRichard Scheffenegger if (t_flags & TF_PREVVALID) { 28563f169c54SRichard Scheffenegger db_printf("%sTF_PREVVALID", comma ? ", " : ""); 28573f169c54SRichard Scheffenegger comma = 1; 28583f169c54SRichard Scheffenegger } 2859497057eeSRobert Watson if (t_flags & TF_MORETOCOME) { 2860497057eeSRobert Watson db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 2861497057eeSRobert Watson comma = 1; 2862497057eeSRobert Watson } 2863493105c2SGleb Smirnoff if (t_flags & TF_SONOTCONN) { 2864493105c2SGleb Smirnoff db_printf("%sTF_SONOTCONN", comma ? ", " : ""); 2865497057eeSRobert Watson comma = 1; 2866497057eeSRobert Watson } 2867497057eeSRobert Watson if (t_flags & TF_LASTIDLE) { 2868497057eeSRobert Watson db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 2869497057eeSRobert Watson comma = 1; 2870497057eeSRobert Watson } 2871497057eeSRobert Watson if (t_flags & TF_RXWIN0SENT) { 2872497057eeSRobert Watson db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 2873497057eeSRobert Watson comma = 1; 2874497057eeSRobert Watson } 2875497057eeSRobert Watson if (t_flags & TF_FASTRECOVERY) { 2876497057eeSRobert Watson db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 2877497057eeSRobert Watson comma = 1; 2878497057eeSRobert Watson } 2879dbc42409SLawrence Stewart if (t_flags & TF_CONGRECOVERY) { 2880dbc42409SLawrence Stewart db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 2881dbc42409SLawrence Stewart comma = 1; 2882dbc42409SLawrence Stewart } 2883497057eeSRobert Watson if (t_flags & TF_WASFRECOVERY) { 2884497057eeSRobert Watson db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 2885497057eeSRobert Watson comma = 1; 2886497057eeSRobert Watson } 28873f169c54SRichard Scheffenegger if (t_flags & TF_WASCRECOVERY) { 28883f169c54SRichard Scheffenegger db_printf("%sTF_WASCRECOVERY", comma ? ", " : ""); 28893f169c54SRichard Scheffenegger comma = 1; 28903f169c54SRichard Scheffenegger } 2891497057eeSRobert Watson if (t_flags & TF_SIGNATURE) { 2892497057eeSRobert Watson db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 2893497057eeSRobert Watson comma = 1; 2894497057eeSRobert Watson } 2895497057eeSRobert Watson if (t_flags & TF_FORCEDATA) { 2896497057eeSRobert Watson db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 2897497057eeSRobert Watson comma = 1; 2898497057eeSRobert Watson } 2899497057eeSRobert Watson if (t_flags & TF_TSO) { 2900497057eeSRobert Watson db_printf("%sTF_TSO", comma ? ", " : ""); 2901497057eeSRobert Watson comma = 1; 2902497057eeSRobert Watson } 2903281a0fd4SPatrick Kelsey if (t_flags & TF_FASTOPEN) { 2904281a0fd4SPatrick Kelsey db_printf("%sTF_FASTOPEN", comma ? ", " : ""); 2905281a0fd4SPatrick Kelsey comma = 1; 2906281a0fd4SPatrick Kelsey } 2907497057eeSRobert Watson } 2908497057eeSRobert Watson 2909497057eeSRobert Watson static void 29103cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2) 29113cf38784SMichael Tuexen { 29123cf38784SMichael Tuexen int comma; 29133cf38784SMichael Tuexen 29143cf38784SMichael Tuexen comma = 0; 29153f169c54SRichard Scheffenegger if (t_flags2 & TF2_PLPMTU_BLACKHOLE) { 29163f169c54SRichard Scheffenegger db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : ""); 29173f169c54SRichard Scheffenegger comma = 1; 29183f169c54SRichard Scheffenegger } 29193f169c54SRichard Scheffenegger if (t_flags2 & TF2_PLPMTU_PMTUD) { 29203f169c54SRichard Scheffenegger db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : ""); 29213f169c54SRichard Scheffenegger comma = 1; 29223f169c54SRichard Scheffenegger } 29233f169c54SRichard Scheffenegger if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) { 29243f169c54SRichard Scheffenegger db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : ""); 29253f169c54SRichard Scheffenegger comma = 1; 29263f169c54SRichard Scheffenegger } 29273f169c54SRichard Scheffenegger if (t_flags2 & TF2_LOG_AUTO) { 29283f169c54SRichard Scheffenegger db_printf("%sTF2_LOG_AUTO", comma ? ", " : ""); 29293f169c54SRichard Scheffenegger comma = 1; 29303f169c54SRichard Scheffenegger } 29313f169c54SRichard Scheffenegger if (t_flags2 & TF2_DROP_AF_DATA) { 29323f169c54SRichard Scheffenegger db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : ""); 29333f169c54SRichard Scheffenegger comma = 1; 29343f169c54SRichard Scheffenegger } 29353cf38784SMichael Tuexen if (t_flags2 & TF2_ECN_PERMIT) { 29363cf38784SMichael Tuexen db_printf("%sTF2_ECN_PERMIT", comma ? ", " : ""); 29373cf38784SMichael Tuexen comma = 1; 29383cf38784SMichael Tuexen } 29393f169c54SRichard Scheffenegger if (t_flags2 & TF2_ECN_SND_CWR) { 29403f169c54SRichard Scheffenegger db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : ""); 29413f169c54SRichard Scheffenegger comma = 1; 29423f169c54SRichard Scheffenegger } 29433f169c54SRichard Scheffenegger if (t_flags2 & TF2_ECN_SND_ECE) { 29443f169c54SRichard Scheffenegger db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : ""); 29453f169c54SRichard Scheffenegger comma = 1; 29463f169c54SRichard Scheffenegger } 29473f169c54SRichard Scheffenegger if (t_flags2 & TF2_ACE_PERMIT) { 29483f169c54SRichard Scheffenegger db_printf("%sTF2_ACE_PERMIT", comma ? ", " : ""); 29493f169c54SRichard Scheffenegger comma = 1; 29503f169c54SRichard Scheffenegger } 29513f169c54SRichard Scheffenegger if (t_flags2 & TF2_FBYTES_COMPLETE) { 29523f169c54SRichard Scheffenegger db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : ""); 29533f169c54SRichard Scheffenegger comma = 1; 29543f169c54SRichard Scheffenegger } 29553cf38784SMichael Tuexen } 29563cf38784SMichael Tuexen 29573cf38784SMichael Tuexen static void 2958497057eeSRobert Watson db_print_toobflags(char t_oobflags) 2959497057eeSRobert Watson { 2960497057eeSRobert Watson int comma; 2961497057eeSRobert Watson 2962497057eeSRobert Watson comma = 0; 2963497057eeSRobert Watson if (t_oobflags & TCPOOB_HAVEDATA) { 2964497057eeSRobert Watson db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 2965497057eeSRobert Watson comma = 1; 2966497057eeSRobert Watson } 2967497057eeSRobert Watson if (t_oobflags & TCPOOB_HADDATA) { 2968497057eeSRobert Watson db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 2969497057eeSRobert Watson comma = 1; 2970497057eeSRobert Watson } 2971497057eeSRobert Watson } 2972497057eeSRobert Watson 2973497057eeSRobert Watson static void 2974497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 2975497057eeSRobert Watson { 2976497057eeSRobert Watson 2977497057eeSRobert Watson db_print_indent(indent); 2978497057eeSRobert Watson db_printf("%s at %p\n", name, tp); 2979497057eeSRobert Watson 2980497057eeSRobert Watson indent += 2; 2981497057eeSRobert Watson 2982497057eeSRobert Watson db_print_indent(indent); 2983497057eeSRobert Watson db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 2984c28440dbSRandall Stewart TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 2985497057eeSRobert Watson 2986497057eeSRobert Watson db_print_indent(indent); 2987446ccdd0SGleb Smirnoff db_printf("t_callout: %p t_timers: %p\n", 2988446ccdd0SGleb Smirnoff &tp->t_callout, &tp->t_timers); 2989497057eeSRobert Watson 2990497057eeSRobert Watson db_print_indent(indent); 2991497057eeSRobert Watson db_printf("t_state: %d (", tp->t_state); 2992497057eeSRobert Watson db_print_tstate(tp->t_state); 2993497057eeSRobert Watson db_printf(")\n"); 2994497057eeSRobert Watson 2995497057eeSRobert Watson db_print_indent(indent); 2996497057eeSRobert Watson db_printf("t_flags: 0x%x (", tp->t_flags); 2997497057eeSRobert Watson db_print_tflags(tp->t_flags); 2998497057eeSRobert Watson db_printf(")\n"); 2999497057eeSRobert Watson 3000497057eeSRobert Watson db_print_indent(indent); 30013cf38784SMichael Tuexen db_printf("t_flags2: 0x%x (", tp->t_flags2); 30023cf38784SMichael Tuexen db_print_tflags2(tp->t_flags2); 30033cf38784SMichael Tuexen db_printf(")\n"); 30043cf38784SMichael Tuexen 30053cf38784SMichael Tuexen db_print_indent(indent); 3006497057eeSRobert Watson db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 3007497057eeSRobert Watson tp->snd_una, tp->snd_max, tp->snd_nxt); 3008497057eeSRobert Watson 3009497057eeSRobert Watson db_print_indent(indent); 3010497057eeSRobert Watson db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 3011497057eeSRobert Watson tp->snd_up, tp->snd_wl1, tp->snd_wl2); 3012497057eeSRobert Watson 3013497057eeSRobert Watson db_print_indent(indent); 3014497057eeSRobert Watson db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 3015497057eeSRobert Watson tp->iss, tp->irs, tp->rcv_nxt); 3016497057eeSRobert Watson 3017497057eeSRobert Watson db_print_indent(indent); 30183ac12506SJonathan T. Looney db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n", 3019497057eeSRobert Watson tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 3020497057eeSRobert Watson 3021497057eeSRobert Watson db_print_indent(indent); 30223ac12506SJonathan T. Looney db_printf("snd_wnd: %u snd_cwnd: %u\n", 30231c18314dSAndre Oppermann tp->snd_wnd, tp->snd_cwnd); 3024497057eeSRobert Watson 3025497057eeSRobert Watson db_print_indent(indent); 30263ac12506SJonathan T. Looney db_printf("snd_ssthresh: %u snd_recover: " 30271c18314dSAndre Oppermann "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 3028497057eeSRobert Watson 3029497057eeSRobert Watson db_print_indent(indent); 30300c39d38dSGleb Smirnoff db_printf("t_rcvtime: %u t_startime: %u\n", 30310c39d38dSGleb Smirnoff tp->t_rcvtime, tp->t_starttime); 3032497057eeSRobert Watson 3033497057eeSRobert Watson db_print_indent(indent); 30341c18314dSAndre Oppermann db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 30351c18314dSAndre Oppermann tp->t_rtttime, tp->t_rtseq); 3036497057eeSRobert Watson 3037497057eeSRobert Watson db_print_indent(indent); 30381c18314dSAndre Oppermann db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 30391c18314dSAndre Oppermann tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 3040497057eeSRobert Watson 3041497057eeSRobert Watson db_print_indent(indent); 3042bd4f9866SMichael Tuexen db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u\n", 3043bd4f9866SMichael Tuexen tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin); 3044497057eeSRobert Watson 3045497057eeSRobert Watson db_print_indent(indent); 304618b83b62SRichard Scheffenegger db_printf("t_rttupdated: %u max_sndwnd: %u t_softerror: %d\n", 3047497057eeSRobert Watson tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 3048497057eeSRobert Watson 3049497057eeSRobert Watson db_print_indent(indent); 3050497057eeSRobert Watson db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 3051497057eeSRobert Watson db_print_toobflags(tp->t_oobflags); 3052497057eeSRobert Watson db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 3053497057eeSRobert Watson 3054497057eeSRobert Watson db_print_indent(indent); 3055497057eeSRobert Watson db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 3056497057eeSRobert Watson tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 3057497057eeSRobert Watson 3058497057eeSRobert Watson db_print_indent(indent); 30599f78a87aSJohn Baldwin db_printf("ts_recent: %u ts_recent_age: %u\n", 30601a553740SAndre Oppermann tp->ts_recent, tp->ts_recent_age); 3061497057eeSRobert Watson 3062497057eeSRobert Watson db_print_indent(indent); 3063497057eeSRobert Watson db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 30643ac12506SJonathan T. Looney "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 3065497057eeSRobert Watson 3066497057eeSRobert Watson db_print_indent(indent); 30673ac12506SJonathan T. Looney db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x " 30689f78a87aSJohn Baldwin "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 3069497057eeSRobert Watson tp->snd_recover_prev, tp->t_badrxtwin); 3070497057eeSRobert Watson 3071497057eeSRobert Watson db_print_indent(indent); 30723529149eSAndre Oppermann db_printf("snd_numholes: %d snd_holes first: %p\n", 30733529149eSAndre Oppermann tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 3074497057eeSRobert Watson 3075497057eeSRobert Watson db_print_indent(indent); 3076a3574665SMichael Tuexen db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n", 3077a3574665SMichael Tuexen tp->snd_fack, tp->rcv_numsacks); 3078497057eeSRobert Watson 3079497057eeSRobert Watson /* Skip sackblks, sackhint. */ 3080497057eeSRobert Watson 3081497057eeSRobert Watson db_print_indent(indent); 3082497057eeSRobert Watson db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 3083497057eeSRobert Watson tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 3084497057eeSRobert Watson } 3085497057eeSRobert Watson 3086497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 3087497057eeSRobert Watson { 3088497057eeSRobert Watson struct tcpcb *tp; 3089497057eeSRobert Watson 3090497057eeSRobert Watson if (!have_addr) { 3091497057eeSRobert Watson db_printf("usage: show tcpcb <addr>\n"); 3092497057eeSRobert Watson return; 3093497057eeSRobert Watson } 3094497057eeSRobert Watson tp = (struct tcpcb *)addr; 3095497057eeSRobert Watson 3096497057eeSRobert Watson db_print_tcpcb(tp, "tcpcb", 0); 3097497057eeSRobert Watson } 3098497057eeSRobert Watson #endif 3099