1c398230bSWarner Losh /*- 2*51369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*51369649SPedro 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" 470cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 480cc12cc5SJoerg Wunsch 49df8bae1dSRodney W. Grimes #include <sys/param.h> 50df8bae1dSRodney W. Grimes #include <sys/systm.h> 519077f387SGleb Smirnoff #include <sys/limits.h> 52f76fcf6dSJeffrey Hsu #include <sys/malloc.h> 5355bceb1eSRandall Stewart #include <sys/refcount.h> 54c7a82f90SGarrett Wollman #include <sys/kernel.h> 5598163b98SPoul-Henning Kamp #include <sys/sysctl.h> 56df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 57fb59c426SYoshinobu Inoue #ifdef INET6 58fb59c426SYoshinobu Inoue #include <sys/domain.h> 59fb59c426SYoshinobu Inoue #endif /* INET6 */ 60df8bae1dSRodney W. Grimes #include <sys/socket.h> 61df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 62df8bae1dSRodney W. Grimes #include <sys/protosw.h> 6391421ba2SRobert Watson #include <sys/proc.h> 6491421ba2SRobert Watson #include <sys/jail.h> 65f5cf1e5fSJulien Charbon #include <sys/syslog.h> 66df8bae1dSRodney W. Grimes 67497057eeSRobert Watson #ifdef DDB 68497057eeSRobert Watson #include <ddb/ddb.h> 69497057eeSRobert Watson #endif 70497057eeSRobert Watson 71df8bae1dSRodney W. Grimes #include <net/if.h> 7276039bc8SGleb Smirnoff #include <net/if_var.h> 73df8bae1dSRodney W. Grimes #include <net/route.h> 74530c0060SRobert Watson #include <net/vnet.h> 75df8bae1dSRodney W. Grimes 76df8bae1dSRodney W. Grimes #include <netinet/in.h> 775d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h> 78df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 79b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h> 80b5e8ce9fSBruce Evans #include <netinet/in_var.h> 81df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 82fb59c426SYoshinobu Inoue #ifdef INET6 83b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h> 84b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 85fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 86a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h> 87fb59c426SYoshinobu Inoue #endif 88281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 89281a0fd4SPatrick Kelsey #include <netinet/tcp_fastopen.h> 90281a0fd4SPatrick Kelsey #endif 912de3e790SGleb Smirnoff #include <netinet/tcp.h> 92df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 93df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 94df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 95df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 96df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 974644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 9886a996e6SHiren Panchasara #ifdef TCPPCAP 9986a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 10086a996e6SHiren Panchasara #endif 101610ee2f9SDavid Greenman #ifdef TCPDEBUG 102df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 103610ee2f9SDavid Greenman #endif 10409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 105bc65987aSKip Macy #include <netinet/tcp_offload.h> 10609fe6320SNavdeep Parhar #endif 107fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 108df8bae1dSRodney W. Grimes 109df8bae1dSRodney W. Grimes /* 110df8bae1dSRodney W. Grimes * TCP protocol interface to socket abstraction. 111df8bae1dSRodney W. Grimes */ 11256dc72c3SPawel Jakub Dawidek static int tcp_attach(struct socket *); 113b287c6c7SBjoern A. Zeeb #ifdef INET 1144d77a549SAlfred Perlstein static int tcp_connect(struct tcpcb *, struct sockaddr *, 1154d77a549SAlfred Perlstein struct thread *td); 116b287c6c7SBjoern A. Zeeb #endif /* INET */ 117fb59c426SYoshinobu Inoue #ifdef INET6 1184d77a549SAlfred Perlstein static int tcp6_connect(struct tcpcb *, struct sockaddr *, 1194d77a549SAlfred Perlstein struct thread *td); 120fb59c426SYoshinobu Inoue #endif /* INET6 */ 121623dce13SRobert Watson static void tcp_disconnect(struct tcpcb *); 122623dce13SRobert Watson static void tcp_usrclosed(struct tcpcb *); 123b8af5dfaSRobert Watson static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 1242c37256eSGarrett Wollman 1252c37256eSGarrett Wollman #ifdef TCPDEBUG 1261db24ffbSJonathan Lemon #define TCPDEBUG0 int ostate = 0 1272c37256eSGarrett Wollman #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 1284cc20ab1SSeigo Tanimura #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 1294cc20ab1SSeigo Tanimura tcp_trace(TA_USER, ostate, tp, 0, 0, req) 1302c37256eSGarrett Wollman #else 1312c37256eSGarrett Wollman #define TCPDEBUG0 1322c37256eSGarrett Wollman #define TCPDEBUG1() 1332c37256eSGarrett Wollman #define TCPDEBUG2(req) 1342c37256eSGarrett Wollman #endif 1352c37256eSGarrett Wollman 1362c37256eSGarrett Wollman /* 1372c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 1382c37256eSGarrett Wollman * and an internet control block. 1392c37256eSGarrett Wollman */ 1402c37256eSGarrett Wollman static int 141b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td) 1422c37256eSGarrett Wollman { 143f76fcf6dSJeffrey Hsu struct inpcb *inp; 144623dce13SRobert Watson struct tcpcb *tp = NULL; 145623dce13SRobert Watson int error; 1462c37256eSGarrett Wollman TCPDEBUG0; 1472c37256eSGarrett Wollman 148623dce13SRobert Watson inp = sotoinpcb(so); 149623dce13SRobert Watson KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 1502c37256eSGarrett Wollman TCPDEBUG1(); 1512c37256eSGarrett Wollman 15256dc72c3SPawel Jakub Dawidek error = tcp_attach(so); 1532c37256eSGarrett Wollman if (error) 1542c37256eSGarrett Wollman goto out; 1552c37256eSGarrett Wollman 1562c37256eSGarrett Wollman if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1573879597fSAndrey A. Chernov so->so_linger = TCP_LINGERTIME; 158f76fcf6dSJeffrey Hsu 159f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 160f76fcf6dSJeffrey Hsu tp = intotcpcb(inp); 1612c37256eSGarrett Wollman out: 1622c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 1635d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ATTACH); 1642c37256eSGarrett Wollman return error; 1652c37256eSGarrett Wollman } 1662c37256eSGarrett Wollman 1672c37256eSGarrett Wollman /* 168a152f8a3SRobert Watson * tcp_detach is called when the socket layer loses its final reference 169a152f8a3SRobert Watson * to the socket, be it a file descriptor reference, a reference from TCP, 170a152f8a3SRobert Watson * etc. At this point, there is only one case in which we will keep around 171a152f8a3SRobert Watson * inpcb state: time wait. 172c78cbc7bSRobert Watson * 173a152f8a3SRobert Watson * This function can probably be re-absorbed back into tcp_usr_detach() now 174a152f8a3SRobert Watson * that there is a single detach path. 1752c37256eSGarrett Wollman */ 176bc725eafSRobert Watson static void 177c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp) 1782c37256eSGarrett Wollman { 1792c37256eSGarrett Wollman struct tcpcb *tp; 1802c37256eSGarrett Wollman 181079672cbSJulien Charbon INP_INFO_LOCK_ASSERT(&V_tcbinfo); 1828501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 183623dce13SRobert Watson 184c78cbc7bSRobert Watson KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp")); 185c78cbc7bSRobert Watson KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so")); 186953b5606SRobert Watson 187a152f8a3SRobert Watson tp = intotcpcb(inp); 188a152f8a3SRobert Watson 189ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 190623dce13SRobert Watson /* 191a152f8a3SRobert Watson * There are two cases to handle: one in which the time wait 192a152f8a3SRobert Watson * state is being discarded (INP_DROPPED), and one in which 193a152f8a3SRobert Watson * this connection will remain in timewait. In the former, 194a152f8a3SRobert Watson * it is time to discard all state (except tcptw, which has 195a152f8a3SRobert Watson * already been discarded by the timewait close code, which 196a152f8a3SRobert Watson * should be further up the call stack somewhere). In the 197a152f8a3SRobert Watson * latter case, we detach from the socket, but leave the pcb 198a152f8a3SRobert Watson * present until timewait ends. 199623dce13SRobert Watson * 200a152f8a3SRobert Watson * XXXRW: Would it be cleaner to free the tcptw here? 201cea40c48SJulien Charbon * 202cea40c48SJulien Charbon * Astute question indeed, from twtcp perspective there are 203cea40c48SJulien Charbon * three cases to consider: 204cea40c48SJulien Charbon * 205cea40c48SJulien Charbon * #1 tcp_detach is called at tcptw creation time by 206cea40c48SJulien Charbon * tcp_twstart, then do not discard the newly created tcptw 207cea40c48SJulien Charbon * and leave inpcb present until timewait ends 208cea40c48SJulien Charbon * #2 tcp_detach is called at timewait end (or reuse) by 209cea40c48SJulien Charbon * tcp_twclose, then the tcptw has already been discarded 210ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 211cea40c48SJulien Charbon * #3 tcp_detach is called() after timewait ends (or reuse) 212cea40c48SJulien Charbon * (e.g. by soclose), then tcptw has already been discarded 213ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 214cea40c48SJulien Charbon * 215cea40c48SJulien Charbon * In all three cases the tcptw should not be freed here. 216623dce13SRobert Watson */ 217ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED) { 218623dce13SRobert Watson in_pcbdetach(inp); 219f5cf1e5fSJulien Charbon if (__predict_true(tp == NULL)) { 2200206cdb8SBjoern A. Zeeb in_pcbfree(inp); 2210206cdb8SBjoern A. Zeeb } else { 222f5cf1e5fSJulien Charbon /* 223f5cf1e5fSJulien Charbon * This case should not happen as in TIMEWAIT 224f5cf1e5fSJulien Charbon * state the inp should not be destroyed before 225f5cf1e5fSJulien Charbon * its tcptw. If INVARIANTS is defined, panic. 226f5cf1e5fSJulien Charbon */ 227f5cf1e5fSJulien Charbon #ifdef INVARIANTS 228f5cf1e5fSJulien Charbon panic("%s: Panic before an inp double-free: " 229f5cf1e5fSJulien Charbon "INP_TIMEWAIT && INP_DROPPED && tp != NULL" 230f5cf1e5fSJulien Charbon , __func__); 231f5cf1e5fSJulien Charbon #else 232f5cf1e5fSJulien Charbon log(LOG_ERR, "%s: Avoid an inp double-free: " 233f5cf1e5fSJulien Charbon "INP_TIMEWAIT && INP_DROPPED && tp != NULL" 234f5cf1e5fSJulien Charbon , __func__); 235f5cf1e5fSJulien Charbon #endif 236f5cf1e5fSJulien Charbon INP_WUNLOCK(inp); 237f5cf1e5fSJulien Charbon } 238f5cf1e5fSJulien Charbon } else { 239623dce13SRobert Watson in_pcbdetach(inp); 2408501a69cSRobert Watson INP_WUNLOCK(inp); 241623dce13SRobert Watson } 242623dce13SRobert Watson } else { 243e6e65783SRobert Watson /* 244a152f8a3SRobert Watson * If the connection is not in timewait, we consider two 245a152f8a3SRobert Watson * two conditions: one in which no further processing is 246a152f8a3SRobert Watson * necessary (dropped || embryonic), and one in which TCP is 247a152f8a3SRobert Watson * not yet done, but no longer requires the socket, so the 248a152f8a3SRobert Watson * pcb will persist for the time being. 249a152f8a3SRobert Watson * 250a152f8a3SRobert Watson * XXXRW: Does the second case still occur? 251e6e65783SRobert Watson */ 252ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED || 253623dce13SRobert Watson tp->t_state < TCPS_SYN_SENT) { 254623dce13SRobert Watson tcp_discardcb(tp); 255623dce13SRobert Watson in_pcbdetach(inp); 2560206cdb8SBjoern A. Zeeb in_pcbfree(inp); 257db3cee51SNavdeep Parhar } else { 258a152f8a3SRobert Watson in_pcbdetach(inp); 259db3cee51SNavdeep Parhar INP_WUNLOCK(inp); 260db3cee51SNavdeep Parhar } 261623dce13SRobert Watson } 262623dce13SRobert Watson } 263c78cbc7bSRobert Watson 264c78cbc7bSRobert Watson /* 265c78cbc7bSRobert Watson * pru_detach() detaches the TCP protocol from the socket. 266c78cbc7bSRobert Watson * If the protocol state is non-embryonic, then can't 267c78cbc7bSRobert Watson * do this directly: have to initiate a pru_disconnect(), 268c78cbc7bSRobert Watson * which may finish later; embryonic TCB's can just 269c78cbc7bSRobert Watson * be discarded here. 270c78cbc7bSRobert Watson */ 271c78cbc7bSRobert Watson static void 272c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so) 273c78cbc7bSRobert Watson { 274c78cbc7bSRobert Watson struct inpcb *inp; 275079672cbSJulien Charbon int rlock = 0; 276c78cbc7bSRobert Watson 277c78cbc7bSRobert Watson inp = sotoinpcb(so); 278c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL")); 279079672cbSJulien Charbon if (!INP_INFO_WLOCKED(&V_tcbinfo)) { 280ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 281079672cbSJulien Charbon rlock = 1; 282079672cbSJulien Charbon } 2838501a69cSRobert Watson INP_WLOCK(inp); 284c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 285c78cbc7bSRobert Watson ("tcp_usr_detach: inp_socket == NULL")); 286c78cbc7bSRobert Watson tcp_detach(so, inp); 287079672cbSJulien Charbon if (rlock) 288ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 2892c37256eSGarrett Wollman } 2902c37256eSGarrett Wollman 291b287c6c7SBjoern A. Zeeb #ifdef INET 2922c37256eSGarrett Wollman /* 2932c37256eSGarrett Wollman * Give the socket an address. 2942c37256eSGarrett Wollman */ 2952c37256eSGarrett Wollman static int 296b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 2972c37256eSGarrett Wollman { 2982c37256eSGarrett Wollman int error = 0; 299f76fcf6dSJeffrey Hsu struct inpcb *inp; 300623dce13SRobert Watson struct tcpcb *tp = NULL; 3012c37256eSGarrett Wollman struct sockaddr_in *sinp; 3022c37256eSGarrett Wollman 30352710de1SPawel Jakub Dawidek sinp = (struct sockaddr_in *)nam; 30452710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sinp)) 30552710de1SPawel Jakub Dawidek return (EINVAL); 3062c37256eSGarrett Wollman /* 3072c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 3082c37256eSGarrett Wollman * to them. 3092c37256eSGarrett Wollman */ 3102c37256eSGarrett Wollman if (sinp->sin_family == AF_INET && 31152710de1SPawel Jakub Dawidek IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 31252710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 31352710de1SPawel Jakub Dawidek 314623dce13SRobert Watson TCPDEBUG0; 315623dce13SRobert Watson inp = sotoinpcb(so); 316623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 3178501a69cSRobert Watson INP_WLOCK(inp); 318ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 319623dce13SRobert Watson error = EINVAL; 3202c37256eSGarrett Wollman goto out; 321623dce13SRobert Watson } 322623dce13SRobert Watson tp = intotcpcb(inp); 323623dce13SRobert Watson TCPDEBUG1(); 324fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 325623dce13SRobert Watson error = in_pcbbind(inp, nam, td->td_ucred); 326fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 327623dce13SRobert Watson out: 328623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 3295d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 3308501a69cSRobert Watson INP_WUNLOCK(inp); 331623dce13SRobert Watson 332623dce13SRobert Watson return (error); 3332c37256eSGarrett Wollman } 334b287c6c7SBjoern A. Zeeb #endif /* INET */ 3352c37256eSGarrett Wollman 336fb59c426SYoshinobu Inoue #ifdef INET6 337fb59c426SYoshinobu Inoue static int 338b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 339fb59c426SYoshinobu Inoue { 340fb59c426SYoshinobu Inoue int error = 0; 341f76fcf6dSJeffrey Hsu struct inpcb *inp; 342623dce13SRobert Watson struct tcpcb *tp = NULL; 343fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 344fb59c426SYoshinobu Inoue 34552710de1SPawel Jakub Dawidek sin6p = (struct sockaddr_in6 *)nam; 34652710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sin6p)) 34752710de1SPawel Jakub Dawidek return (EINVAL); 348fb59c426SYoshinobu Inoue /* 349fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 350fb59c426SYoshinobu Inoue * to them. 351fb59c426SYoshinobu Inoue */ 352fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 && 35352710de1SPawel Jakub Dawidek IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 35452710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 35552710de1SPawel Jakub Dawidek 356623dce13SRobert Watson TCPDEBUG0; 357623dce13SRobert Watson inp = sotoinpcb(so); 358623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 3598501a69cSRobert Watson INP_WLOCK(inp); 360ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 361623dce13SRobert Watson error = EINVAL; 362623dce13SRobert Watson goto out; 363623dce13SRobert Watson } 364623dce13SRobert Watson tp = intotcpcb(inp); 365623dce13SRobert Watson TCPDEBUG1(); 366fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 367fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 368fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 369b287c6c7SBjoern A. Zeeb #ifdef INET 37066ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 371fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 372fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 373fb59c426SYoshinobu Inoue else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 374fb59c426SYoshinobu Inoue struct sockaddr_in sin; 375fb59c426SYoshinobu Inoue 376fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 377fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 378fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 379b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)&sin, 380b0330ed9SPawel Jakub Dawidek td->td_ucred); 381fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 382fb59c426SYoshinobu Inoue goto out; 383fb59c426SYoshinobu Inoue } 384fb59c426SYoshinobu Inoue } 385b287c6c7SBjoern A. Zeeb #endif 386b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, nam, td->td_ucred); 387fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 388623dce13SRobert Watson out: 389623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 3905d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 3918501a69cSRobert Watson INP_WUNLOCK(inp); 392623dce13SRobert Watson return (error); 393fb59c426SYoshinobu Inoue } 394fb59c426SYoshinobu Inoue #endif /* INET6 */ 395fb59c426SYoshinobu Inoue 396b287c6c7SBjoern A. Zeeb #ifdef INET 3972c37256eSGarrett Wollman /* 3982c37256eSGarrett Wollman * Prepare to accept connections. 3992c37256eSGarrett Wollman */ 4002c37256eSGarrett Wollman static int 401d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 4022c37256eSGarrett Wollman { 4032c37256eSGarrett Wollman int error = 0; 404f76fcf6dSJeffrey Hsu struct inpcb *inp; 405623dce13SRobert Watson struct tcpcb *tp = NULL; 4062c37256eSGarrett Wollman 407623dce13SRobert Watson TCPDEBUG0; 408623dce13SRobert Watson inp = sotoinpcb(so); 409623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 4108501a69cSRobert Watson INP_WLOCK(inp); 411ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 412623dce13SRobert Watson error = EINVAL; 413623dce13SRobert Watson goto out; 414623dce13SRobert Watson } 415623dce13SRobert Watson tp = intotcpcb(inp); 416623dce13SRobert Watson TCPDEBUG1(); 4170daccb9cSRobert Watson SOCK_LOCK(so); 4180daccb9cSRobert Watson error = solisten_proto_check(so); 419fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 4200daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) 421b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 422fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 4230daccb9cSRobert Watson if (error == 0) { 42457f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 425d374e81eSRobert Watson solisten_proto(so, backlog); 42609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 42737cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 42809fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 42909fe6320SNavdeep Parhar #endif 4300daccb9cSRobert Watson } 4310daccb9cSRobert Watson SOCK_UNLOCK(so); 432623dce13SRobert Watson 433281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 43468bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) 435281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 436281a0fd4SPatrick Kelsey #endif 437623dce13SRobert Watson out: 438623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 4395d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 4408501a69cSRobert Watson INP_WUNLOCK(inp); 441623dce13SRobert Watson return (error); 4422c37256eSGarrett Wollman } 443b287c6c7SBjoern A. Zeeb #endif /* INET */ 4442c37256eSGarrett Wollman 445fb59c426SYoshinobu Inoue #ifdef INET6 446fb59c426SYoshinobu Inoue static int 447d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 448fb59c426SYoshinobu Inoue { 449fb59c426SYoshinobu Inoue int error = 0; 450f76fcf6dSJeffrey Hsu struct inpcb *inp; 451623dce13SRobert Watson struct tcpcb *tp = NULL; 452fb59c426SYoshinobu Inoue 453623dce13SRobert Watson TCPDEBUG0; 454623dce13SRobert Watson inp = sotoinpcb(so); 455623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 4568501a69cSRobert Watson INP_WLOCK(inp); 457ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 458623dce13SRobert Watson error = EINVAL; 459623dce13SRobert Watson goto out; 460623dce13SRobert Watson } 461623dce13SRobert Watson tp = intotcpcb(inp); 462623dce13SRobert Watson TCPDEBUG1(); 4630daccb9cSRobert Watson SOCK_LOCK(so); 4640daccb9cSRobert Watson error = solisten_proto_check(so); 465fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 4660daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) { 467fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 46866ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 469fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 470b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 471fb59c426SYoshinobu Inoue } 472fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 4730daccb9cSRobert Watson if (error == 0) { 47457f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 475d374e81eSRobert Watson solisten_proto(so, backlog); 47609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 47737cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 47809fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 47909fe6320SNavdeep Parhar #endif 4800daccb9cSRobert Watson } 4810daccb9cSRobert Watson SOCK_UNLOCK(so); 482623dce13SRobert Watson 483281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 48468bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) 485281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 486281a0fd4SPatrick Kelsey #endif 487623dce13SRobert Watson out: 488623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 4895d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 4908501a69cSRobert Watson INP_WUNLOCK(inp); 491623dce13SRobert Watson return (error); 492fb59c426SYoshinobu Inoue } 493fb59c426SYoshinobu Inoue #endif /* INET6 */ 494fb59c426SYoshinobu Inoue 495b287c6c7SBjoern A. Zeeb #ifdef INET 4962c37256eSGarrett Wollman /* 4972c37256eSGarrett Wollman * Initiate connection to peer. 4982c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 4992c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 5002c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 5012c37256eSGarrett Wollman * Send initial segment on connection. 5022c37256eSGarrett Wollman */ 5032c37256eSGarrett Wollman static int 504b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 5052c37256eSGarrett Wollman { 5062c37256eSGarrett Wollman int error = 0; 507f76fcf6dSJeffrey Hsu struct inpcb *inp; 508623dce13SRobert Watson struct tcpcb *tp = NULL; 5092c37256eSGarrett Wollman struct sockaddr_in *sinp; 5102c37256eSGarrett Wollman 51157bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 512e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sinp)) 513e29ef13fSDon Lewis return (EINVAL); 51452710de1SPawel Jakub Dawidek /* 51552710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 51652710de1SPawel Jakub Dawidek */ 5172c37256eSGarrett Wollman if (sinp->sin_family == AF_INET 51852710de1SPawel Jakub Dawidek && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 51952710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 520b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 521b89e82ddSJamie Gritton return (error); 52275c13541SPoul-Henning Kamp 523623dce13SRobert Watson TCPDEBUG0; 524623dce13SRobert Watson inp = sotoinpcb(so); 525623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 5268501a69cSRobert Watson INP_WLOCK(inp); 527eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 528eb96dc33SJulien Charbon error = EADDRINUSE; 529eb96dc33SJulien Charbon goto out; 530eb96dc33SJulien Charbon } 531eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 532eb96dc33SJulien Charbon error = ECONNREFUSED; 533623dce13SRobert Watson goto out; 534623dce13SRobert Watson } 535623dce13SRobert Watson tp = intotcpcb(inp); 536623dce13SRobert Watson TCPDEBUG1(); 537b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, td)) != 0) 5382c37256eSGarrett Wollman goto out; 53909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 54009fe6320SNavdeep Parhar if (registered_toedevs > 0 && 54137cc0ecbSNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 54209fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 54309fe6320SNavdeep Parhar goto out; 54409fe6320SNavdeep Parhar #endif 54509fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 54655bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 547623dce13SRobert Watson out: 548623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 549e79cb051SGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 5508501a69cSRobert Watson INP_WUNLOCK(inp); 551623dce13SRobert Watson return (error); 5522c37256eSGarrett Wollman } 553b287c6c7SBjoern A. Zeeb #endif /* INET */ 5542c37256eSGarrett Wollman 555fb59c426SYoshinobu Inoue #ifdef INET6 556fb59c426SYoshinobu Inoue static int 557b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 558fb59c426SYoshinobu Inoue { 559fb59c426SYoshinobu Inoue int error = 0; 560f76fcf6dSJeffrey Hsu struct inpcb *inp; 561623dce13SRobert Watson struct tcpcb *tp = NULL; 562fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 563623dce13SRobert Watson 564623dce13SRobert Watson TCPDEBUG0; 565fb59c426SYoshinobu Inoue 566fb59c426SYoshinobu Inoue sin6p = (struct sockaddr_in6 *)nam; 567e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sin6p)) 568e29ef13fSDon Lewis return (EINVAL); 56952710de1SPawel Jakub Dawidek /* 57052710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 57152710de1SPawel Jakub Dawidek */ 572fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 57352710de1SPawel Jakub Dawidek && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 57452710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 575fb59c426SYoshinobu Inoue 576623dce13SRobert Watson inp = sotoinpcb(so); 577623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 5788501a69cSRobert Watson INP_WLOCK(inp); 579eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 580eb96dc33SJulien Charbon error = EADDRINUSE; 581eb96dc33SJulien Charbon goto out; 582eb96dc33SJulien Charbon } 583eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 584eb96dc33SJulien Charbon error = ECONNREFUSED; 585623dce13SRobert Watson goto out; 586623dce13SRobert Watson } 587623dce13SRobert Watson tp = intotcpcb(inp); 588623dce13SRobert Watson TCPDEBUG1(); 589b287c6c7SBjoern A. Zeeb #ifdef INET 590fa046d87SRobert Watson /* 591fa046d87SRobert Watson * XXXRW: Some confusion: V4/V6 flags relate to binding, and 592fa046d87SRobert Watson * therefore probably require the hash lock, which isn't held here. 593fa046d87SRobert Watson * Is this a significant problem? 594fa046d87SRobert Watson */ 59533841545SHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 596fb59c426SYoshinobu Inoue struct sockaddr_in sin; 597fb59c426SYoshinobu Inoue 598d46a5312SMaxim Konovalov if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 599d46a5312SMaxim Konovalov error = EINVAL; 600d46a5312SMaxim Konovalov goto out; 601d46a5312SMaxim Konovalov } 6025dba6adaSMichael Tuexen if ((inp->inp_vflag & INP_IPV4) == 0) { 6035dba6adaSMichael Tuexen error = EAFNOSUPPORT; 6045dba6adaSMichael Tuexen goto out; 6055dba6adaSMichael Tuexen } 60633841545SHajimu UMEMOTO 607fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 608fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 609fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 610b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, 611b89e82ddSJamie Gritton &sin.sin_addr)) != 0) 612413628a7SBjoern A. Zeeb goto out; 613b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 614fb59c426SYoshinobu Inoue goto out; 61509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 61609fe6320SNavdeep Parhar if (registered_toedevs > 0 && 617adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 61809fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 61909fe6320SNavdeep Parhar goto out; 62009fe6320SNavdeep Parhar #endif 62155bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 622fb59c426SYoshinobu Inoue goto out; 6235dba6adaSMichael Tuexen } else { 6245dba6adaSMichael Tuexen if ((inp->inp_vflag & INP_IPV6) == 0) { 6255dba6adaSMichael Tuexen error = EAFNOSUPPORT; 6265dba6adaSMichael Tuexen goto out; 6275dba6adaSMichael Tuexen } 628fb59c426SYoshinobu Inoue } 629b287c6c7SBjoern A. Zeeb #endif 630fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 631fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 632dcdb4371SBjoern A. Zeeb inp->inp_inc.inc_flags |= INC_ISIPV6; 633b89e82ddSJamie Gritton if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0) 634413628a7SBjoern A. Zeeb goto out; 635b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, td)) != 0) 636fb59c426SYoshinobu Inoue goto out; 63709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 63809fe6320SNavdeep Parhar if (registered_toedevs > 0 && 639adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 64009fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 64109fe6320SNavdeep Parhar goto out; 64209fe6320SNavdeep Parhar #endif 64309fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 64455bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 645623dce13SRobert Watson 646623dce13SRobert Watson out: 647623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 6485d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 6498501a69cSRobert Watson INP_WUNLOCK(inp); 650623dce13SRobert Watson return (error); 651fb59c426SYoshinobu Inoue } 652fb59c426SYoshinobu Inoue #endif /* INET6 */ 653fb59c426SYoshinobu Inoue 6542c37256eSGarrett Wollman /* 6552c37256eSGarrett Wollman * Initiate disconnect from peer. 6562c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 6572c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 6582c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 6592c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 6602c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 6612c37256eSGarrett Wollman * when peer sends FIN and acks ours. 6622c37256eSGarrett Wollman * 6632c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 6642c37256eSGarrett Wollman */ 6652c37256eSGarrett Wollman static int 6662c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 6672c37256eSGarrett Wollman { 668f76fcf6dSJeffrey Hsu struct inpcb *inp; 669623dce13SRobert Watson struct tcpcb *tp = NULL; 670623dce13SRobert Watson int error = 0; 6712c37256eSGarrett Wollman 672623dce13SRobert Watson TCPDEBUG0; 673ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 674623dce13SRobert Watson inp = sotoinpcb(so); 675623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 6768501a69cSRobert Watson INP_WLOCK(inp); 677489dcc92SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) 678489dcc92SJulien Charbon goto out; 679489dcc92SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 68021367f63SSam Leffler error = ECONNRESET; 681623dce13SRobert Watson goto out; 682623dce13SRobert Watson } 683623dce13SRobert Watson tp = intotcpcb(inp); 684623dce13SRobert Watson TCPDEBUG1(); 685623dce13SRobert Watson tcp_disconnect(tp); 686623dce13SRobert Watson out: 687623dce13SRobert Watson TCPDEBUG2(PRU_DISCONNECT); 6885d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_DISCONNECT); 6898501a69cSRobert Watson INP_WUNLOCK(inp); 690ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 691623dce13SRobert Watson return (error); 6922c37256eSGarrett Wollman } 6932c37256eSGarrett Wollman 694b287c6c7SBjoern A. Zeeb #ifdef INET 6952c37256eSGarrett Wollman /* 6968296cddfSRobert Watson * Accept a connection. Essentially all the work is done at higher levels; 6978296cddfSRobert Watson * just return the address of the peer, storing through addr. 6982c37256eSGarrett Wollman */ 6992c37256eSGarrett Wollman static int 70057bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 7012c37256eSGarrett Wollman { 7022c37256eSGarrett Wollman int error = 0; 703f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 7041db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 70526ef6ac4SDon Lewis struct in_addr addr; 70626ef6ac4SDon Lewis in_port_t port = 0; 7071db24ffbSJonathan Lemon TCPDEBUG0; 7082c37256eSGarrett Wollman 7093d2d3ef4SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 7103d2d3ef4SRobert Watson return (ECONNABORTED); 711f76fcf6dSJeffrey Hsu 712f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 713623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 7148501a69cSRobert Watson INP_WLOCK(inp); 715ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 7163d2d3ef4SRobert Watson error = ECONNABORTED; 717623dce13SRobert Watson goto out; 718623dce13SRobert Watson } 7191db24ffbSJonathan Lemon tp = intotcpcb(inp); 7201db24ffbSJonathan Lemon TCPDEBUG1(); 721f76fcf6dSJeffrey Hsu 722f76fcf6dSJeffrey Hsu /* 72354d642bbSRobert Watson * We inline in_getpeeraddr and COMMON_END here, so that we can 72426ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 72526ef6ac4SDon Lewis * release the lock. 726f76fcf6dSJeffrey Hsu */ 72726ef6ac4SDon Lewis port = inp->inp_fport; 72826ef6ac4SDon Lewis addr = inp->inp_faddr; 729f76fcf6dSJeffrey Hsu 730623dce13SRobert Watson out: 731623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 7325d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 7338501a69cSRobert Watson INP_WUNLOCK(inp); 73426ef6ac4SDon Lewis if (error == 0) 73526ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 73626ef6ac4SDon Lewis return error; 7372c37256eSGarrett Wollman } 738b287c6c7SBjoern A. Zeeb #endif /* INET */ 7392c37256eSGarrett Wollman 740fb59c426SYoshinobu Inoue #ifdef INET6 741fb59c426SYoshinobu Inoue static int 742fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 743fb59c426SYoshinobu Inoue { 744f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 745fb59c426SYoshinobu Inoue int error = 0; 7461db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 74726ef6ac4SDon Lewis struct in_addr addr; 74826ef6ac4SDon Lewis struct in6_addr addr6; 74926ef6ac4SDon Lewis in_port_t port = 0; 75026ef6ac4SDon Lewis int v4 = 0; 7511db24ffbSJonathan Lemon TCPDEBUG0; 752fb59c426SYoshinobu Inoue 753b4470c16SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 754b4470c16SRobert Watson return (ECONNABORTED); 755f76fcf6dSJeffrey Hsu 756f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 757623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 758fa046d87SRobert Watson INP_INFO_RLOCK(&V_tcbinfo); 7598501a69cSRobert Watson INP_WLOCK(inp); 760ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 76121367f63SSam Leffler error = ECONNABORTED; 762623dce13SRobert Watson goto out; 763623dce13SRobert Watson } 7641db24ffbSJonathan Lemon tp = intotcpcb(inp); 7651db24ffbSJonathan Lemon TCPDEBUG1(); 766623dce13SRobert Watson 76726ef6ac4SDon Lewis /* 76826ef6ac4SDon Lewis * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 76926ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 77026ef6ac4SDon Lewis * release the lock. 77126ef6ac4SDon Lewis */ 77226ef6ac4SDon Lewis if (inp->inp_vflag & INP_IPV4) { 77326ef6ac4SDon Lewis v4 = 1; 77426ef6ac4SDon Lewis port = inp->inp_fport; 77526ef6ac4SDon Lewis addr = inp->inp_faddr; 77626ef6ac4SDon Lewis } else { 77726ef6ac4SDon Lewis port = inp->inp_fport; 77826ef6ac4SDon Lewis addr6 = inp->in6p_faddr; 77926ef6ac4SDon Lewis } 78026ef6ac4SDon Lewis 781623dce13SRobert Watson out: 782623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 7835d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 7848501a69cSRobert Watson INP_WUNLOCK(inp); 785fa046d87SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 78626ef6ac4SDon Lewis if (error == 0) { 78726ef6ac4SDon Lewis if (v4) 78826ef6ac4SDon Lewis *nam = in6_v4mapsin6_sockaddr(port, &addr); 78926ef6ac4SDon Lewis else 79026ef6ac4SDon Lewis *nam = in6_sockaddr(port, &addr6); 79126ef6ac4SDon Lewis } 79226ef6ac4SDon Lewis return error; 793fb59c426SYoshinobu Inoue } 794fb59c426SYoshinobu Inoue #endif /* INET6 */ 795f76fcf6dSJeffrey Hsu 796f76fcf6dSJeffrey Hsu /* 7972c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 7982c37256eSGarrett Wollman */ 7992c37256eSGarrett Wollman static int 8002c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 8012c37256eSGarrett Wollman { 8022c37256eSGarrett Wollman int error = 0; 803f76fcf6dSJeffrey Hsu struct inpcb *inp; 804623dce13SRobert Watson struct tcpcb *tp = NULL; 8052c37256eSGarrett Wollman 806623dce13SRobert Watson TCPDEBUG0; 807ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 808623dce13SRobert Watson inp = sotoinpcb(so); 809623dce13SRobert Watson KASSERT(inp != NULL, ("inp == NULL")); 8108501a69cSRobert Watson INP_WLOCK(inp); 811ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 81221367f63SSam Leffler error = ECONNRESET; 813623dce13SRobert Watson goto out; 814623dce13SRobert Watson } 815623dce13SRobert Watson tp = intotcpcb(inp); 816623dce13SRobert Watson TCPDEBUG1(); 8172c37256eSGarrett Wollman socantsendmore(so); 818623dce13SRobert Watson tcp_usrclosed(tp); 819ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 82055bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 821623dce13SRobert Watson 822623dce13SRobert Watson out: 823623dce13SRobert Watson TCPDEBUG2(PRU_SHUTDOWN); 8245d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN); 8258501a69cSRobert Watson INP_WUNLOCK(inp); 826ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 827623dce13SRobert Watson 828623dce13SRobert Watson return (error); 8292c37256eSGarrett Wollman } 8302c37256eSGarrett Wollman 8312c37256eSGarrett Wollman /* 8322c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 8332c37256eSGarrett Wollman */ 8342c37256eSGarrett Wollman static int 8352c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 8362c37256eSGarrett Wollman { 837f76fcf6dSJeffrey Hsu struct inpcb *inp; 838623dce13SRobert Watson struct tcpcb *tp = NULL; 839623dce13SRobert Watson int error = 0; 8402c37256eSGarrett Wollman 841623dce13SRobert Watson TCPDEBUG0; 842623dce13SRobert Watson inp = sotoinpcb(so); 843623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 8448501a69cSRobert Watson INP_WLOCK(inp); 845ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 84621367f63SSam Leffler error = ECONNRESET; 847623dce13SRobert Watson goto out; 848623dce13SRobert Watson } 849623dce13SRobert Watson tp = intotcpcb(inp); 850623dce13SRobert Watson TCPDEBUG1(); 851281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 852281a0fd4SPatrick Kelsey /* 853281a0fd4SPatrick Kelsey * For passively-created TFO connections, don't attempt a window 854281a0fd4SPatrick Kelsey * update while still in SYN_RECEIVED as this may trigger an early 855281a0fd4SPatrick Kelsey * SYN|ACK. It is preferable to have the SYN|ACK be sent along with 856281a0fd4SPatrick Kelsey * application response data, or failing that, when the DELACK timer 857281a0fd4SPatrick Kelsey * expires. 858281a0fd4SPatrick Kelsey */ 85968bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 860281a0fd4SPatrick Kelsey (tp->t_state == TCPS_SYN_RECEIVED)) 861281a0fd4SPatrick Kelsey goto out; 862281a0fd4SPatrick Kelsey #endif 86309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 86409fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 86509fe6320SNavdeep Parhar tcp_offload_rcvd(tp); 866460cf046SNavdeep Parhar else 86709fe6320SNavdeep Parhar #endif 86855bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 869623dce13SRobert Watson 870623dce13SRobert Watson out: 871623dce13SRobert Watson TCPDEBUG2(PRU_RCVD); 8725d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVD); 8738501a69cSRobert Watson INP_WUNLOCK(inp); 874623dce13SRobert Watson return (error); 8752c37256eSGarrett Wollman } 8762c37256eSGarrett Wollman 8772c37256eSGarrett Wollman /* 8782c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 8799c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 8809c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 8819c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 8829c9906e9SPeter Wemm * generally are caller-frees. 8832c37256eSGarrett Wollman */ 8842c37256eSGarrett Wollman static int 88557bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 886b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 8872c37256eSGarrett Wollman { 8882c37256eSGarrett Wollman int error = 0; 889f76fcf6dSJeffrey Hsu struct inpcb *inp; 890623dce13SRobert Watson struct tcpcb *tp = NULL; 891fb59c426SYoshinobu Inoue #ifdef INET6 892fb59c426SYoshinobu Inoue int isipv6; 893fb59c426SYoshinobu Inoue #endif 8949c9906e9SPeter Wemm TCPDEBUG0; 8952c37256eSGarrett Wollman 896f76fcf6dSJeffrey Hsu /* 897fa046d87SRobert Watson * We require the pcbinfo lock if we will close the socket as part of 898fa046d87SRobert Watson * this call. 899f76fcf6dSJeffrey Hsu */ 900fa046d87SRobert Watson if (flags & PRUS_EOF) 901ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 902f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 903623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 9048501a69cSRobert Watson INP_WLOCK(inp); 905ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 9067ff0b850SAndre Oppermann if (control) 9077ff0b850SAndre Oppermann m_freem(control); 9082cbcd3c1SGleb Smirnoff /* 9092cbcd3c1SGleb Smirnoff * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible 9102cbcd3c1SGleb Smirnoff * for freeing memory. 9112cbcd3c1SGleb Smirnoff */ 9122cbcd3c1SGleb Smirnoff if (m && (flags & PRUS_NOTREADY) == 0) 9137ff0b850SAndre Oppermann m_freem(m); 91421367f63SSam Leffler error = ECONNRESET; 9159c9906e9SPeter Wemm goto out; 9169c9906e9SPeter Wemm } 917fb59c426SYoshinobu Inoue #ifdef INET6 918fb59c426SYoshinobu Inoue isipv6 = nam && nam->sa_family == AF_INET6; 919fb59c426SYoshinobu Inoue #endif /* INET6 */ 9209c9906e9SPeter Wemm tp = intotcpcb(inp); 9219c9906e9SPeter Wemm TCPDEBUG1(); 9229c9906e9SPeter Wemm if (control) { 9239c9906e9SPeter Wemm /* TCP doesn't do control messages (rights, creds, etc) */ 9249c9906e9SPeter Wemm if (control->m_len) { 9259c9906e9SPeter Wemm m_freem(control); 9262c37256eSGarrett Wollman if (m) 9272c37256eSGarrett Wollman m_freem(m); 928744f87eaSDavid Greenman error = EINVAL; 929744f87eaSDavid Greenman goto out; 9302c37256eSGarrett Wollman } 9319c9906e9SPeter Wemm m_freem(control); /* empty control, just free it */ 9329c9906e9SPeter Wemm } 9332c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 934651e4e6aSGleb Smirnoff sbappendstream(&so->so_snd, m, flags); 9352c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 9362c37256eSGarrett Wollman /* 9372c37256eSGarrett Wollman * Do implied connect if not yet connected, 9382c37256eSGarrett Wollman * initialize window to default value, and 9390c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 9402c37256eSGarrett Wollman */ 941fb59c426SYoshinobu Inoue #ifdef INET6 942fb59c426SYoshinobu Inoue if (isipv6) 943b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 944fb59c426SYoshinobu Inoue #endif /* INET6 */ 945b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 946b287c6c7SBjoern A. Zeeb else 947b287c6c7SBjoern A. Zeeb #endif 948b287c6c7SBjoern A. Zeeb #ifdef INET 949b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 950b287c6c7SBjoern A. Zeeb #endif 9512c37256eSGarrett Wollman if (error) 9522c37256eSGarrett Wollman goto out; 9532c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 9542c37256eSGarrett Wollman tcp_mss(tp, -1); 9552c37256eSGarrett Wollman } 9562c37256eSGarrett Wollman if (flags & PRUS_EOF) { 9572c37256eSGarrett Wollman /* 9582c37256eSGarrett Wollman * Close the send side of the connection after 9592c37256eSGarrett Wollman * the data is sent. 9602c37256eSGarrett Wollman */ 961ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 9622c37256eSGarrett Wollman socantsendmore(so); 963623dce13SRobert Watson tcp_usrclosed(tp); 9642c37256eSGarrett Wollman } 9652cbcd3c1SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED) && 9662cbcd3c1SGleb Smirnoff !(flags & PRUS_NOTREADY)) { 967b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 968b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 96955bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 970b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 971b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 972b0acefa8SBill Fenner } 9732c37256eSGarrett Wollman } else { 974623dce13SRobert Watson /* 975623dce13SRobert Watson * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 976623dce13SRobert Watson */ 977d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 9782c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 979d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 9802c37256eSGarrett Wollman m_freem(m); 9812c37256eSGarrett Wollman error = ENOBUFS; 9822c37256eSGarrett Wollman goto out; 9832c37256eSGarrett Wollman } 9842c37256eSGarrett Wollman /* 9852c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 9862c37256eSGarrett Wollman * the urgent pointer points to the last octet 9872c37256eSGarrett Wollman * of urgent data. We continue, however, 9882c37256eSGarrett Wollman * to consider it to indicate the first octet 9892c37256eSGarrett Wollman * of data past the urgent section. 9902c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 9912c37256eSGarrett Wollman */ 992651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_snd, m, flags); 993d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 994ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 995ef53690bSGarrett Wollman /* 996ef53690bSGarrett Wollman * Do implied connect if not yet connected, 997ef53690bSGarrett Wollman * initialize window to default value, and 9980c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 999ef53690bSGarrett Wollman */ 1000fb59c426SYoshinobu Inoue #ifdef INET6 1001fb59c426SYoshinobu Inoue if (isipv6) 1002b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 1003fb59c426SYoshinobu Inoue #endif /* INET6 */ 1004b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1005b287c6c7SBjoern A. Zeeb else 1006b287c6c7SBjoern A. Zeeb #endif 1007b287c6c7SBjoern A. Zeeb #ifdef INET 1008b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 1009b287c6c7SBjoern A. Zeeb #endif 1010ef53690bSGarrett Wollman if (error) 1011ef53690bSGarrett Wollman goto out; 1012ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 1013ef53690bSGarrett Wollman tcp_mss(tp, -1); 1014623dce13SRobert Watson } 1015300fa232SGleb Smirnoff tp->snd_up = tp->snd_una + sbavail(&so->so_snd); 10162cbcd3c1SGleb Smirnoff if (!(flags & PRUS_NOTREADY)) { 10172cdbfa66SPaul Saab tp->t_flags |= TF_FORCEDATA; 101855bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 10192cdbfa66SPaul Saab tp->t_flags &= ~TF_FORCEDATA; 10202c37256eSGarrett Wollman } 10212cbcd3c1SGleb Smirnoff } 1022d1401c90SRobert Watson out: 1023d1401c90SRobert Watson TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 10242c37256eSGarrett Wollman ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 10255d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB : 10265d06879aSGeorge V. Neville-Neil ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 10278501a69cSRobert Watson INP_WUNLOCK(inp); 1028fa046d87SRobert Watson if (flags & PRUS_EOF) 1029ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 103073fddedaSPeter Grehan return (error); 10312c37256eSGarrett Wollman } 10322c37256eSGarrett Wollman 10332cbcd3c1SGleb Smirnoff static int 10342cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count) 10352cbcd3c1SGleb Smirnoff { 10362cbcd3c1SGleb Smirnoff struct inpcb *inp; 10372cbcd3c1SGleb Smirnoff struct tcpcb *tp; 10382cbcd3c1SGleb Smirnoff int error; 10392cbcd3c1SGleb Smirnoff 10402cbcd3c1SGleb Smirnoff inp = sotoinpcb(so); 10412cbcd3c1SGleb Smirnoff INP_WLOCK(inp); 10422cbcd3c1SGleb Smirnoff if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 10432cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 10442cbcd3c1SGleb Smirnoff for (int i = 0; i < count; i++) 10452cbcd3c1SGleb Smirnoff m = m_free(m); 10462cbcd3c1SGleb Smirnoff return (ECONNRESET); 10472cbcd3c1SGleb Smirnoff } 10482cbcd3c1SGleb Smirnoff tp = intotcpcb(inp); 10492cbcd3c1SGleb Smirnoff 10502cbcd3c1SGleb Smirnoff SOCKBUF_LOCK(&so->so_snd); 10512cbcd3c1SGleb Smirnoff error = sbready(&so->so_snd, m, count); 10522cbcd3c1SGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 10532cbcd3c1SGleb Smirnoff if (error == 0) 105455bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 10552cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 10562cbcd3c1SGleb Smirnoff 10572cbcd3c1SGleb Smirnoff return (error); 10582cbcd3c1SGleb Smirnoff } 10592cbcd3c1SGleb Smirnoff 10602c37256eSGarrett Wollman /* 1061a152f8a3SRobert Watson * Abort the TCP. Drop the connection abruptly. 10622c37256eSGarrett Wollman */ 1063ac45e92fSRobert Watson static void 10642c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 10652c37256eSGarrett Wollman { 1066f76fcf6dSJeffrey Hsu struct inpcb *inp; 1067a152f8a3SRobert Watson struct tcpcb *tp = NULL; 1068623dce13SRobert Watson TCPDEBUG0; 1069c78cbc7bSRobert Watson 1070ac45e92fSRobert Watson inp = sotoinpcb(so); 1071c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 1072c78cbc7bSRobert Watson 1073ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 10748501a69cSRobert Watson INP_WLOCK(inp); 1075c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 1076c78cbc7bSRobert Watson ("tcp_usr_abort: inp_socket == NULL")); 1077c78cbc7bSRobert Watson 1078c78cbc7bSRobert Watson /* 1079a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, drop. 1080c78cbc7bSRobert Watson */ 1081ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 1082ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 1083c78cbc7bSRobert Watson tp = intotcpcb(inp); 1084a152f8a3SRobert Watson TCPDEBUG1(); 1085c78cbc7bSRobert Watson tcp_drop(tp, ECONNABORTED); 1086a152f8a3SRobert Watson TCPDEBUG2(PRU_ABORT); 10875d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ABORT); 1088c78cbc7bSRobert Watson } 1089ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1090a152f8a3SRobert Watson SOCK_LOCK(so); 1091a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 1092a152f8a3SRobert Watson SOCK_UNLOCK(so); 1093ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1094a152f8a3SRobert Watson } 10958501a69cSRobert Watson INP_WUNLOCK(inp); 1096ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1097a152f8a3SRobert Watson } 1098a152f8a3SRobert Watson 1099a152f8a3SRobert Watson /* 1100a152f8a3SRobert Watson * TCP socket is closed. Start friendly disconnect. 1101a152f8a3SRobert Watson */ 1102a152f8a3SRobert Watson static void 1103a152f8a3SRobert Watson tcp_usr_close(struct socket *so) 1104a152f8a3SRobert Watson { 1105a152f8a3SRobert Watson struct inpcb *inp; 1106a152f8a3SRobert Watson struct tcpcb *tp = NULL; 1107a152f8a3SRobert Watson TCPDEBUG0; 1108a152f8a3SRobert Watson 1109a152f8a3SRobert Watson inp = sotoinpcb(so); 1110a152f8a3SRobert Watson KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 1111a152f8a3SRobert Watson 1112ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 11138501a69cSRobert Watson INP_WLOCK(inp); 1114a152f8a3SRobert Watson KASSERT(inp->inp_socket != NULL, 1115a152f8a3SRobert Watson ("tcp_usr_close: inp_socket == NULL")); 1116a152f8a3SRobert Watson 1117a152f8a3SRobert Watson /* 1118a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, initiate 1119a152f8a3SRobert Watson * a disconnect. 1120a152f8a3SRobert Watson */ 1121ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 1122ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 1123a152f8a3SRobert Watson tp = intotcpcb(inp); 1124a152f8a3SRobert Watson TCPDEBUG1(); 1125a152f8a3SRobert Watson tcp_disconnect(tp); 1126a152f8a3SRobert Watson TCPDEBUG2(PRU_CLOSE); 11275d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CLOSE); 1128a152f8a3SRobert Watson } 1129ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1130a152f8a3SRobert Watson SOCK_LOCK(so); 1131a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 1132a152f8a3SRobert Watson SOCK_UNLOCK(so); 1133ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1134a152f8a3SRobert Watson } 11358501a69cSRobert Watson INP_WUNLOCK(inp); 1136ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 11372c37256eSGarrett Wollman } 11382c37256eSGarrett Wollman 11392c37256eSGarrett Wollman /* 11402c37256eSGarrett Wollman * Receive out-of-band data. 11412c37256eSGarrett Wollman */ 11422c37256eSGarrett Wollman static int 11432c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 11442c37256eSGarrett Wollman { 11452c37256eSGarrett Wollman int error = 0; 1146f76fcf6dSJeffrey Hsu struct inpcb *inp; 1147623dce13SRobert Watson struct tcpcb *tp = NULL; 11482c37256eSGarrett Wollman 1149623dce13SRobert Watson TCPDEBUG0; 1150623dce13SRobert Watson inp = sotoinpcb(so); 1151623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 11528501a69cSRobert Watson INP_WLOCK(inp); 1153ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 115421367f63SSam Leffler error = ECONNRESET; 1155623dce13SRobert Watson goto out; 1156623dce13SRobert Watson } 1157623dce13SRobert Watson tp = intotcpcb(inp); 1158623dce13SRobert Watson TCPDEBUG1(); 11592c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 1160c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 11614cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 11624cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 11632c37256eSGarrett Wollman error = EINVAL; 11642c37256eSGarrett Wollman goto out; 11652c37256eSGarrett Wollman } 11662c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 11672c37256eSGarrett Wollman error = EWOULDBLOCK; 11682c37256eSGarrett Wollman goto out; 11692c37256eSGarrett Wollman } 11702c37256eSGarrett Wollman m->m_len = 1; 11712c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 11722c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 11732c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1174623dce13SRobert Watson 1175623dce13SRobert Watson out: 1176623dce13SRobert Watson TCPDEBUG2(PRU_RCVOOB); 11775d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVOOB); 11788501a69cSRobert Watson INP_WUNLOCK(inp); 1179623dce13SRobert Watson return (error); 11802c37256eSGarrett Wollman } 11812c37256eSGarrett Wollman 1182b287c6c7SBjoern A. Zeeb #ifdef INET 11832c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = { 1184756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1185756d52a1SPoul-Henning Kamp .pru_accept = tcp_usr_accept, 1186756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1187756d52a1SPoul-Henning Kamp .pru_bind = tcp_usr_bind, 1188756d52a1SPoul-Henning Kamp .pru_connect = tcp_usr_connect, 1189756d52a1SPoul-Henning Kamp .pru_control = in_control, 1190756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1191756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1192756d52a1SPoul-Henning Kamp .pru_listen = tcp_usr_listen, 119354d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1194756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1195756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1196756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 11972cbcd3c1SGleb Smirnoff .pru_ready = tcp_usr_ready, 1198756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 119954d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1200a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1201a152f8a3SRobert Watson .pru_close = tcp_usr_close, 12022c37256eSGarrett Wollman }; 1203b287c6c7SBjoern A. Zeeb #endif /* INET */ 1204df8bae1dSRodney W. Grimes 1205fb59c426SYoshinobu Inoue #ifdef INET6 1206fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = { 1207756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1208756d52a1SPoul-Henning Kamp .pru_accept = tcp6_usr_accept, 1209756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1210756d52a1SPoul-Henning Kamp .pru_bind = tcp6_usr_bind, 1211756d52a1SPoul-Henning Kamp .pru_connect = tcp6_usr_connect, 1212756d52a1SPoul-Henning Kamp .pru_control = in6_control, 1213756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1214756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1215756d52a1SPoul-Henning Kamp .pru_listen = tcp6_usr_listen, 1216756d52a1SPoul-Henning Kamp .pru_peeraddr = in6_mapped_peeraddr, 1217756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1218756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1219756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 12202cbcd3c1SGleb Smirnoff .pru_ready = tcp_usr_ready, 1221756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 1222756d52a1SPoul-Henning Kamp .pru_sockaddr = in6_mapped_sockaddr, 1223a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1224a152f8a3SRobert Watson .pru_close = tcp_usr_close, 1225fb59c426SYoshinobu Inoue }; 1226fb59c426SYoshinobu Inoue #endif /* INET6 */ 1227fb59c426SYoshinobu Inoue 1228b287c6c7SBjoern A. Zeeb #ifdef INET 1229a0292f23SGarrett Wollman /* 1230a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 1231a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 12325200e00eSIan Dowse * port number if needed. Call in_pcbconnect_setup to do the routing and 12335200e00eSIan Dowse * to choose a local host address (interface). If there is an existing 12345200e00eSIan Dowse * incarnation of the same connection in TIME-WAIT state and if the remote 12355200e00eSIan Dowse * host was sending CC options and if the connection duration was < MSL, then 1236a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 1237a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 1238a0292f23SGarrett Wollman */ 12390312fbe9SPoul-Henning Kamp static int 1240ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1241a0292f23SGarrett Wollman { 1242a0292f23SGarrett Wollman struct inpcb *inp = tp->t_inpcb, *oinp; 1243a0292f23SGarrett Wollman struct socket *so = inp->inp_socket; 12445200e00eSIan Dowse struct in_addr laddr; 12455200e00eSIan Dowse u_short lport; 1246c3229e05SDavid Greenman int error; 1247a0292f23SGarrett Wollman 12488501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1249fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1250623dce13SRobert Watson 1251a0292f23SGarrett Wollman if (inp->inp_lport == 0) { 12524616026fSErmal Luçi error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 12534616026fSErmal Luçi if (error) 1254fa046d87SRobert Watson goto out; 1255a0292f23SGarrett Wollman } 1256a0292f23SGarrett Wollman 1257a0292f23SGarrett Wollman /* 1258a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 1259a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 1260a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 1261a0292f23SGarrett Wollman */ 12625200e00eSIan Dowse laddr = inp->inp_laddr; 12635200e00eSIan Dowse lport = inp->inp_lport; 12645200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1265b0330ed9SPawel Jakub Dawidek &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 12665200e00eSIan Dowse if (error && oinp == NULL) 1267fa046d87SRobert Watson goto out; 1268fa046d87SRobert Watson if (oinp) { 1269fa046d87SRobert Watson error = EADDRINUSE; 1270fa046d87SRobert Watson goto out; 1271fa046d87SRobert Watson } 12725200e00eSIan Dowse inp->inp_laddr = laddr; 127315bd2b43SDavid Greenman in_pcbrehash(inp); 1274fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1275a0292f23SGarrett Wollman 1276087b55eaSAndre Oppermann /* 1277087b55eaSAndre Oppermann * Compute window scaling to request: 1278087b55eaSAndre Oppermann * Scale to fit into sweet spot. See tcp_syncache.c. 1279087b55eaSAndre Oppermann * XXX: This should move to tcp_output(). 1280087b55eaSAndre Oppermann */ 1281a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 12829b3bc6bfSMike Silbersack (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1283a0292f23SGarrett Wollman tp->request_r_scale++; 1284a0292f23SGarrett Wollman 1285a0292f23SGarrett Wollman soisconnecting(so); 128678b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 128757f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 1288b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 1289a0292f23SGarrett Wollman tcp_sendseqinit(tp); 1290a45d2726SAndras Olah 1291a0292f23SGarrett Wollman return 0; 1292fa046d87SRobert Watson 1293fa046d87SRobert Watson out: 1294fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1295fa046d87SRobert Watson return (error); 1296a0292f23SGarrett Wollman } 1297b287c6c7SBjoern A. Zeeb #endif /* INET */ 1298a0292f23SGarrett Wollman 1299fb59c426SYoshinobu Inoue #ifdef INET6 1300fb59c426SYoshinobu Inoue static int 1301ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1302fb59c426SYoshinobu Inoue { 1303a7e201bbSAndrey V. Elsukov struct inpcb *inp = tp->t_inpcb; 1304fb59c426SYoshinobu Inoue int error; 1305fb59c426SYoshinobu Inoue 13068501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1307fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1308623dce13SRobert Watson 1309fb59c426SYoshinobu Inoue if (inp->inp_lport == 0) { 13104616026fSErmal Luçi error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 13114616026fSErmal Luçi if (error) 1312fa046d87SRobert Watson goto out; 1313fb59c426SYoshinobu Inoue } 1314a7e201bbSAndrey V. Elsukov error = in6_pcbconnect(inp, nam, td->td_ucred); 1315a7e201bbSAndrey V. Elsukov if (error != 0) 1316b598155aSRobert Watson goto out; 1317fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1318fb59c426SYoshinobu Inoue 1319fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1320fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1321970caf60SBjoern A. Zeeb (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1322fb59c426SYoshinobu Inoue tp->request_r_scale++; 1323fb59c426SYoshinobu Inoue 1324a7e201bbSAndrey V. Elsukov soisconnecting(inp->inp_socket); 132578b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 132657f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 1327b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 1328fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1329fb59c426SYoshinobu Inoue 1330fb59c426SYoshinobu Inoue return 0; 1331fa046d87SRobert Watson 1332fa046d87SRobert Watson out: 1333fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1334fa046d87SRobert Watson return error; 1335fb59c426SYoshinobu Inoue } 1336fb59c426SYoshinobu Inoue #endif /* INET6 */ 1337fb59c426SYoshinobu Inoue 1338cfe8b629SGarrett Wollman /* 1339b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1340b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1341b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1342b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1343b8af5dfaSRobert Watson * from Linux. 1344b8af5dfaSRobert Watson */ 1345b8af5dfaSRobert Watson static void 1346ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1347b8af5dfaSRobert Watson { 1348b8af5dfaSRobert Watson 13498501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1350b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1351b8af5dfaSRobert Watson 1352b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1353b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1354b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 13553529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 1356b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1357b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1358b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1359b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1360b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1361b8af5dfaSRobert Watson } 13625a17b6adSMichael Tuexen if (tp->t_flags & TF_ECN_PERMIT) 13635a17b6adSMichael Tuexen ti->tcpi_options |= TCPI_OPT_ECN; 13641baaf834SBruce M Simpson 136543d94734SJohn Baldwin ti->tcpi_rto = tp->t_rxtcur * tick; 13663ac12506SJonathan T. Looney ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick; 13671baaf834SBruce M Simpson ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 13681baaf834SBruce M Simpson ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 13691baaf834SBruce M Simpson 1370b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1371b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1372b8af5dfaSRobert Watson 1373b8af5dfaSRobert Watson /* 1374b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1375b8af5dfaSRobert Watson */ 1376c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1377535fbad6SKip Macy ti->tcpi_rcv_nxt = tp->rcv_nxt; 1378b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 13791c18314dSAndre Oppermann ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1380535fbad6SKip Macy ti->tcpi_snd_nxt = tp->snd_nxt; 138143d94734SJohn Baldwin ti->tcpi_snd_mss = tp->t_maxseg; 138243d94734SJohn Baldwin ti->tcpi_rcv_mss = tp->t_maxseg; 1383535fbad6SKip Macy if (tp->t_flags & TF_TOE) 1384535fbad6SKip Macy ti->tcpi_options |= TCPI_OPT_TOE; 1385f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1386f5d34df5SGeorge V. Neville-Neil ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1387f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1388b8af5dfaSRobert Watson } 1389b8af5dfaSRobert Watson 1390b8af5dfaSRobert Watson /* 13911e8f5ffaSRobert Watson * tcp_ctloutput() must drop the inpcb lock before performing copyin on 13921e8f5ffaSRobert Watson * socket option arguments. When it re-acquires the lock after the copy, it 13931e8f5ffaSRobert Watson * has to revalidate that the connection is still valid for the socket 13941e8f5ffaSRobert Watson * option. 1395cfe8b629SGarrett Wollman */ 1396bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \ 13978501a69cSRobert Watson INP_WLOCK(inp); \ 1398ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 13998501a69cSRobert Watson INP_WUNLOCK(inp); \ 1400bac5bedfSConrad Meyer cleanup; \ 14011e8f5ffaSRobert Watson return (ECONNRESET); \ 14021e8f5ffaSRobert Watson } \ 14031e8f5ffaSRobert Watson tp = intotcpcb(inp); \ 14041e8f5ffaSRobert Watson } while(0) 1405bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */) 14061e8f5ffaSRobert Watson 1407df8bae1dSRodney W. Grimes int 1408ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1409df8bae1dSRodney W. Grimes { 141055bceb1eSRandall Stewart int error; 1411df8bae1dSRodney W. Grimes struct inpcb *inp; 1412cfe8b629SGarrett Wollman struct tcpcb *tp; 141355bceb1eSRandall Stewart struct tcp_function_block *blk; 141455bceb1eSRandall Stewart struct tcp_function_set fsn; 1415df8bae1dSRodney W. Grimes 1416cfe8b629SGarrett Wollman error = 0; 1417df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1418623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 14198501a69cSRobert Watson INP_WLOCK(inp); 1420cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 1421fb59c426SYoshinobu Inoue #ifdef INET6 14225cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 14238501a69cSRobert Watson INP_WUNLOCK(inp); 1424fb59c426SYoshinobu Inoue error = ip6_ctloutput(so, sopt); 1425b287c6c7SBjoern A. Zeeb } 1426fb59c426SYoshinobu Inoue #endif /* INET6 */ 1427b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1428b287c6c7SBjoern A. Zeeb else 1429b287c6c7SBjoern A. Zeeb #endif 1430b287c6c7SBjoern A. Zeeb #ifdef INET 1431b287c6c7SBjoern A. Zeeb { 14328501a69cSRobert Watson INP_WUNLOCK(inp); 1433cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 14341e8f5ffaSRobert Watson } 14351e8f5ffaSRobert Watson #endif 1436df8bae1dSRodney W. Grimes return (error); 1437df8bae1dSRodney W. Grimes } 1438ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 14398501a69cSRobert Watson INP_WUNLOCK(inp); 14401e8f5ffaSRobert Watson return (ECONNRESET); 1441623dce13SRobert Watson } 144255bceb1eSRandall Stewart tp = intotcpcb(inp); 144355bceb1eSRandall Stewart /* 144455bceb1eSRandall Stewart * Protect the TCP option TCP_FUNCTION_BLK so 144555bceb1eSRandall Stewart * that a sub-function can *never* overwrite this. 144655bceb1eSRandall Stewart */ 144755bceb1eSRandall Stewart if ((sopt->sopt_dir == SOPT_SET) && 144855bceb1eSRandall Stewart (sopt->sopt_name == TCP_FUNCTION_BLK)) { 144955bceb1eSRandall Stewart INP_WUNLOCK(inp); 145055bceb1eSRandall Stewart error = sooptcopyin(sopt, &fsn, sizeof fsn, 145155bceb1eSRandall Stewart sizeof fsn); 145255bceb1eSRandall Stewart if (error) 145355bceb1eSRandall Stewart return (error); 145455bceb1eSRandall Stewart INP_WLOCK_RECHECK(inp); 145555bceb1eSRandall Stewart blk = find_and_ref_tcp_functions(&fsn); 145655bceb1eSRandall Stewart if (blk == NULL) { 145755bceb1eSRandall Stewart INP_WUNLOCK(inp); 145855bceb1eSRandall Stewart return (ENOENT); 145955bceb1eSRandall Stewart } 1460587d67c0SRandall Stewart if (tp->t_fb == blk) { 1461587d67c0SRandall Stewart /* You already have this */ 1462587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1463587d67c0SRandall Stewart INP_WUNLOCK(inp); 1464587d67c0SRandall Stewart return (0); 1465587d67c0SRandall Stewart } 1466587d67c0SRandall Stewart if (tp->t_state != TCPS_CLOSED) { 1467587d67c0SRandall Stewart int error=EINVAL; 1468587d67c0SRandall Stewart /* 1469587d67c0SRandall Stewart * The user has advanced the state 1470587d67c0SRandall Stewart * past the initial point, we may not 1471587d67c0SRandall Stewart * be able to switch. 1472587d67c0SRandall Stewart */ 1473587d67c0SRandall Stewart if (blk->tfb_tcp_handoff_ok != NULL) { 1474587d67c0SRandall Stewart /* 1475587d67c0SRandall Stewart * Does the stack provide a 1476587d67c0SRandall Stewart * query mechanism, if so it may 1477587d67c0SRandall Stewart * still be possible? 1478587d67c0SRandall Stewart */ 1479587d67c0SRandall Stewart error = (*blk->tfb_tcp_handoff_ok)(tp); 1480587d67c0SRandall Stewart } 1481587d67c0SRandall Stewart if (error) { 1482587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1483587d67c0SRandall Stewart INP_WUNLOCK(inp); 1484587d67c0SRandall Stewart return(error); 1485587d67c0SRandall Stewart } 1486587d67c0SRandall Stewart } 148755bceb1eSRandall Stewart if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) { 148855bceb1eSRandall Stewart refcount_release(&blk->tfb_refcnt); 148955bceb1eSRandall Stewart INP_WUNLOCK(inp); 149055bceb1eSRandall Stewart return (ENOENT); 149155bceb1eSRandall Stewart } 149255bceb1eSRandall Stewart /* 149355bceb1eSRandall Stewart * Release the old refcnt, the 1494587d67c0SRandall Stewart * lookup acquired a ref on the 1495587d67c0SRandall Stewart * new one already. 149655bceb1eSRandall Stewart */ 1497587d67c0SRandall Stewart if (tp->t_fb->tfb_tcp_fb_fini) { 1498587d67c0SRandall Stewart /* 1499587d67c0SRandall Stewart * Tell the stack to cleanup with 0 i.e. 1500587d67c0SRandall Stewart * the tcb is not going away. 1501587d67c0SRandall Stewart */ 1502587d67c0SRandall Stewart (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0); 1503587d67c0SRandall Stewart } 150455bceb1eSRandall Stewart refcount_release(&tp->t_fb->tfb_refcnt); 150555bceb1eSRandall Stewart tp->t_fb = blk; 150655bceb1eSRandall Stewart if (tp->t_fb->tfb_tcp_fb_init) { 150755bceb1eSRandall Stewart (*tp->t_fb->tfb_tcp_fb_init)(tp); 150855bceb1eSRandall Stewart } 150955bceb1eSRandall Stewart #ifdef TCP_OFFLOAD 151055bceb1eSRandall Stewart if (tp->t_flags & TF_TOE) { 151155bceb1eSRandall Stewart tcp_offload_ctloutput(tp, sopt->sopt_dir, 151255bceb1eSRandall Stewart sopt->sopt_name); 151355bceb1eSRandall Stewart } 151455bceb1eSRandall Stewart #endif 151555bceb1eSRandall Stewart INP_WUNLOCK(inp); 151655bceb1eSRandall Stewart return (error); 151755bceb1eSRandall Stewart } else if ((sopt->sopt_dir == SOPT_GET) && 151855bceb1eSRandall Stewart (sopt->sopt_name == TCP_FUNCTION_BLK)) { 151955bceb1eSRandall Stewart strcpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name); 152055bceb1eSRandall Stewart fsn.pcbcnt = tp->t_fb->tfb_refcnt; 152155bceb1eSRandall Stewart INP_WUNLOCK(inp); 152255bceb1eSRandall Stewart error = sooptcopyout(sopt, &fsn, sizeof fsn); 152355bceb1eSRandall Stewart return (error); 152455bceb1eSRandall Stewart } 152555bceb1eSRandall Stewart /* Pass in the INP locked, called must unlock it */ 152655bceb1eSRandall Stewart return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp)); 152755bceb1eSRandall Stewart } 152855bceb1eSRandall Stewart 152955bceb1eSRandall Stewart int 153055bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp) 153155bceb1eSRandall Stewart { 153255bceb1eSRandall Stewart int error, opt, optval; 153355bceb1eSRandall Stewart u_int ui; 153455bceb1eSRandall Stewart struct tcp_info ti; 153555bceb1eSRandall Stewart struct cc_algo *algo; 1536af6fef3aSGleb Smirnoff char *pbuf, buf[TCP_CA_NAME_MAX]; 1537af6fef3aSGleb Smirnoff size_t len; 1538df8bae1dSRodney W. Grimes 1539d519cedbSGleb Smirnoff /* 1540d519cedbSGleb Smirnoff * For TCP_CCALGOOPT forward the control to CC module, for both 1541d519cedbSGleb Smirnoff * SOPT_SET and SOPT_GET. 1542d519cedbSGleb Smirnoff */ 1543d519cedbSGleb Smirnoff switch (sopt->sopt_name) { 1544d519cedbSGleb Smirnoff case TCP_CCALGOOPT: 1545d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 1546af6fef3aSGleb Smirnoff pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO); 1547af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize, 1548d519cedbSGleb Smirnoff sopt->sopt_valsize); 1549d519cedbSGleb Smirnoff if (error) { 1550af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 1551d519cedbSGleb Smirnoff return (error); 1552d519cedbSGleb Smirnoff } 1553bac5bedfSConrad Meyer INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP)); 1554d519cedbSGleb Smirnoff if (CC_ALGO(tp)->ctl_output != NULL) 1555af6fef3aSGleb Smirnoff error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf); 1556d519cedbSGleb Smirnoff else 1557d519cedbSGleb Smirnoff error = ENOENT; 1558d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 1559d519cedbSGleb Smirnoff if (error == 0 && sopt->sopt_dir == SOPT_GET) 1560af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize); 1561af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 1562d519cedbSGleb Smirnoff return (error); 1563d519cedbSGleb Smirnoff } 1564d519cedbSGleb Smirnoff 1565cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1566cfe8b629SGarrett Wollman case SOPT_SET: 1567cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1568fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 156988f6b043SBruce M Simpson case TCP_MD5SIG: 1570fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED()) { 15718501a69cSRobert Watson INP_WUNLOCK(inp); 1572fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 1573fcf59617SAndrey V. Elsukov } 1574fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 15751cfd4b53SBruce M Simpson if (error) 15761e8f5ffaSRobert Watson return (error); 157709fe6320SNavdeep Parhar goto unlock_and_done; 1578fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 157909fe6320SNavdeep Parhar 1580df8bae1dSRodney W. Grimes case TCP_NODELAY: 1581cfe8b629SGarrett Wollman case TCP_NOOPT: 15828501a69cSRobert Watson INP_WUNLOCK(inp); 1583cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1584cfe8b629SGarrett Wollman sizeof optval); 1585cfe8b629SGarrett Wollman if (error) 15861e8f5ffaSRobert Watson return (error); 1587cfe8b629SGarrett Wollman 15888501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1589cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1590cfe8b629SGarrett Wollman case TCP_NODELAY: 1591cfe8b629SGarrett Wollman opt = TF_NODELAY; 1592cfe8b629SGarrett Wollman break; 1593cfe8b629SGarrett Wollman case TCP_NOOPT: 1594cfe8b629SGarrett Wollman opt = TF_NOOPT; 1595cfe8b629SGarrett Wollman break; 1596cfe8b629SGarrett Wollman default: 1597cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 1598cfe8b629SGarrett Wollman break; 1599cfe8b629SGarrett Wollman } 1600cfe8b629SGarrett Wollman 1601cfe8b629SGarrett Wollman if (optval) 1602cfe8b629SGarrett Wollman tp->t_flags |= opt; 1603df8bae1dSRodney W. Grimes else 1604cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 160509fe6320SNavdeep Parhar unlock_and_done: 160609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 160709fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 160809fe6320SNavdeep Parhar tcp_offload_ctloutput(tp, sopt->sopt_dir, 160909fe6320SNavdeep Parhar sopt->sopt_name); 161009fe6320SNavdeep Parhar } 161109fe6320SNavdeep Parhar #endif 16128501a69cSRobert Watson INP_WUNLOCK(inp); 1613df8bae1dSRodney W. Grimes break; 1614df8bae1dSRodney W. Grimes 1615007581c0SJonathan Lemon case TCP_NOPUSH: 16168501a69cSRobert Watson INP_WUNLOCK(inp); 1617007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 1618007581c0SJonathan Lemon sizeof optval); 1619007581c0SJonathan Lemon if (error) 16201e8f5ffaSRobert Watson return (error); 1621007581c0SJonathan Lemon 16228501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1623007581c0SJonathan Lemon if (optval) 1624007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 1625d28b9e89SJohn Baldwin else if (tp->t_flags & TF_NOPUSH) { 1626007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 1627d28b9e89SJohn Baldwin if (TCPS_HAVEESTABLISHED(tp->t_state)) 162855bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 1629007581c0SJonathan Lemon } 163009fe6320SNavdeep Parhar goto unlock_and_done; 1631007581c0SJonathan Lemon 1632df8bae1dSRodney W. Grimes case TCP_MAXSEG: 16338501a69cSRobert Watson INP_WUNLOCK(inp); 1634cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1635cfe8b629SGarrett Wollman sizeof optval); 1636cfe8b629SGarrett Wollman if (error) 16371e8f5ffaSRobert Watson return (error); 1638df8bae1dSRodney W. Grimes 16398501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 164053369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 1641603724d3SBjoern A. Zeeb optval + 40 >= V_tcp_minmss) 1642cfe8b629SGarrett Wollman tp->t_maxseg = optval; 1643a0292f23SGarrett Wollman else 1644a0292f23SGarrett Wollman error = EINVAL; 164509fe6320SNavdeep Parhar goto unlock_and_done; 1646a0292f23SGarrett Wollman 1647b8af5dfaSRobert Watson case TCP_INFO: 16488501a69cSRobert Watson INP_WUNLOCK(inp); 1649b8af5dfaSRobert Watson error = EINVAL; 1650b8af5dfaSRobert Watson break; 1651b8af5dfaSRobert Watson 1652dbc42409SLawrence Stewart case TCP_CONGESTION: 1653dbc42409SLawrence Stewart INP_WUNLOCK(inp); 1654af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1); 1655af6fef3aSGleb Smirnoff if (error) 1656dbc42409SLawrence Stewart break; 1657af6fef3aSGleb Smirnoff buf[sopt->sopt_valsize] = '\0'; 1658af6fef3aSGleb Smirnoff INP_WLOCK_RECHECK(inp); 165973e263b1SGleb Smirnoff CC_LIST_RLOCK(); 166073e263b1SGleb Smirnoff STAILQ_FOREACH(algo, &cc_list, entries) 166173e263b1SGleb Smirnoff if (strncmp(buf, algo->name, 166273e263b1SGleb Smirnoff TCP_CA_NAME_MAX) == 0) 166373e263b1SGleb Smirnoff break; 166473e263b1SGleb Smirnoff CC_LIST_RUNLOCK(); 166573e263b1SGleb Smirnoff if (algo == NULL) { 1666af6fef3aSGleb Smirnoff INP_WUNLOCK(inp); 166773e263b1SGleb Smirnoff error = EINVAL; 166873e263b1SGleb Smirnoff break; 166973e263b1SGleb Smirnoff } 1670dbc42409SLawrence Stewart /* 167173e263b1SGleb Smirnoff * We hold a write lock over the tcb so it's safe to 167273e263b1SGleb Smirnoff * do these things without ordering concerns. 1673dbc42409SLawrence Stewart */ 1674dbc42409SLawrence Stewart if (CC_ALGO(tp)->cb_destroy != NULL) 1675dbc42409SLawrence Stewart CC_ALGO(tp)->cb_destroy(tp->ccv); 1676dbc42409SLawrence Stewart CC_ALGO(tp) = algo; 1677dbc42409SLawrence Stewart /* 167873e263b1SGleb Smirnoff * If something goes pear shaped initialising the new 167973e263b1SGleb Smirnoff * algo, fall back to newreno (which does not 168073e263b1SGleb Smirnoff * require initialisation). 1681dbc42409SLawrence Stewart */ 168273e263b1SGleb Smirnoff if (algo->cb_init != NULL && 168373e263b1SGleb Smirnoff algo->cb_init(tp->ccv) != 0) { 1684dbc42409SLawrence Stewart CC_ALGO(tp) = &newreno_cc_algo; 1685dbc42409SLawrence Stewart /* 168673e263b1SGleb Smirnoff * The only reason init should fail is 1687dbc42409SLawrence Stewart * because of malloc. 1688dbc42409SLawrence Stewart */ 1689dbc42409SLawrence Stewart error = ENOMEM; 1690dbc42409SLawrence Stewart } 169173e263b1SGleb Smirnoff INP_WUNLOCK(inp); 169273e263b1SGleb Smirnoff break; 1693dbc42409SLawrence Stewart 16949077f387SGleb Smirnoff case TCP_KEEPIDLE: 16959077f387SGleb Smirnoff case TCP_KEEPINTVL: 16969077f387SGleb Smirnoff case TCP_KEEPINIT: 16979077f387SGleb Smirnoff INP_WUNLOCK(inp); 16989077f387SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 16999077f387SGleb Smirnoff if (error) 17009077f387SGleb Smirnoff return (error); 17019077f387SGleb Smirnoff 17029077f387SGleb Smirnoff if (ui > (UINT_MAX / hz)) { 17039077f387SGleb Smirnoff error = EINVAL; 17049077f387SGleb Smirnoff break; 17059077f387SGleb Smirnoff } 17069077f387SGleb Smirnoff ui *= hz; 17079077f387SGleb Smirnoff 17089077f387SGleb Smirnoff INP_WLOCK_RECHECK(inp); 17099077f387SGleb Smirnoff switch (sopt->sopt_name) { 17109077f387SGleb Smirnoff case TCP_KEEPIDLE: 17119077f387SGleb Smirnoff tp->t_keepidle = ui; 17129077f387SGleb Smirnoff /* 17139077f387SGleb Smirnoff * XXX: better check current remaining 17149077f387SGleb Smirnoff * timeout and "merge" it with new value. 17159077f387SGleb Smirnoff */ 17169077f387SGleb Smirnoff if ((tp->t_state > TCPS_LISTEN) && 17179077f387SGleb Smirnoff (tp->t_state <= TCPS_CLOSING)) 17189077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 17199077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 17209077f387SGleb Smirnoff break; 17219077f387SGleb Smirnoff case TCP_KEEPINTVL: 17229077f387SGleb Smirnoff tp->t_keepintvl = ui; 17239077f387SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 17249077f387SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 17259077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 17269077f387SGleb Smirnoff TP_MAXIDLE(tp)); 17279077f387SGleb Smirnoff break; 17289077f387SGleb Smirnoff case TCP_KEEPINIT: 17299077f387SGleb Smirnoff tp->t_keepinit = ui; 17309077f387SGleb Smirnoff if (tp->t_state == TCPS_SYN_RECEIVED || 17319077f387SGleb Smirnoff tp->t_state == TCPS_SYN_SENT) 17329077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 17339077f387SGleb Smirnoff TP_KEEPINIT(tp)); 17349077f387SGleb Smirnoff break; 17359077f387SGleb Smirnoff } 173609fe6320SNavdeep Parhar goto unlock_and_done; 17379077f387SGleb Smirnoff 173885c05144SGleb Smirnoff case TCP_KEEPCNT: 173985c05144SGleb Smirnoff INP_WUNLOCK(inp); 174085c05144SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 174185c05144SGleb Smirnoff if (error) 174285c05144SGleb Smirnoff return (error); 174385c05144SGleb Smirnoff 174485c05144SGleb Smirnoff INP_WLOCK_RECHECK(inp); 174585c05144SGleb Smirnoff tp->t_keepcnt = ui; 174685c05144SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 174785c05144SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 174885c05144SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 174985c05144SGleb Smirnoff TP_MAXIDLE(tp)); 175085c05144SGleb Smirnoff goto unlock_and_done; 175185c05144SGleb Smirnoff 175286a996e6SHiren Panchasara #ifdef TCPPCAP 175386a996e6SHiren Panchasara case TCP_PCAP_OUT: 175486a996e6SHiren Panchasara case TCP_PCAP_IN: 175586a996e6SHiren Panchasara INP_WUNLOCK(inp); 175686a996e6SHiren Panchasara error = sooptcopyin(sopt, &optval, sizeof optval, 175786a996e6SHiren Panchasara sizeof optval); 175886a996e6SHiren Panchasara if (error) 175986a996e6SHiren Panchasara return (error); 176086a996e6SHiren Panchasara 176186a996e6SHiren Panchasara INP_WLOCK_RECHECK(inp); 176286a996e6SHiren Panchasara if (optval >= 0) 176386a996e6SHiren Panchasara tcp_pcap_set_sock_max(TCP_PCAP_OUT ? 176486a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts), 176586a996e6SHiren Panchasara optval); 176686a996e6SHiren Panchasara else 176786a996e6SHiren Panchasara error = EINVAL; 176886a996e6SHiren Panchasara goto unlock_and_done; 176986a996e6SHiren Panchasara #endif 177086a996e6SHiren Panchasara 1771281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 1772281a0fd4SPatrick Kelsey case TCP_FASTOPEN: 1773281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 1774281a0fd4SPatrick Kelsey if (!V_tcp_fastopen_enabled) 1775281a0fd4SPatrick Kelsey return (EPERM); 1776281a0fd4SPatrick Kelsey 1777281a0fd4SPatrick Kelsey error = sooptcopyin(sopt, &optval, sizeof optval, 1778281a0fd4SPatrick Kelsey sizeof optval); 1779281a0fd4SPatrick Kelsey if (error) 1780281a0fd4SPatrick Kelsey return (error); 1781281a0fd4SPatrick Kelsey 1782281a0fd4SPatrick Kelsey INP_WLOCK_RECHECK(inp); 1783281a0fd4SPatrick Kelsey if (optval) { 1784281a0fd4SPatrick Kelsey tp->t_flags |= TF_FASTOPEN; 1785281a0fd4SPatrick Kelsey if ((tp->t_state == TCPS_LISTEN) && 1786281a0fd4SPatrick Kelsey (tp->t_tfo_pending == NULL)) 1787281a0fd4SPatrick Kelsey tp->t_tfo_pending = 1788281a0fd4SPatrick Kelsey tcp_fastopen_alloc_counter(); 1789281a0fd4SPatrick Kelsey } else 1790281a0fd4SPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 1791281a0fd4SPatrick Kelsey goto unlock_and_done; 1792281a0fd4SPatrick Kelsey #endif 1793281a0fd4SPatrick Kelsey 1794df8bae1dSRodney W. Grimes default: 17958501a69cSRobert Watson INP_WUNLOCK(inp); 1796df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1797df8bae1dSRodney W. Grimes break; 1798df8bae1dSRodney W. Grimes } 1799df8bae1dSRodney W. Grimes break; 1800df8bae1dSRodney W. Grimes 1801cfe8b629SGarrett Wollman case SOPT_GET: 18021e8f5ffaSRobert Watson tp = intotcpcb(inp); 1803cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1804fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 180588f6b043SBruce M Simpson case TCP_MD5SIG: 1806fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED()) { 18078501a69cSRobert Watson INP_WUNLOCK(inp); 1808fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 1809fcf59617SAndrey V. Elsukov } 1810fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 18111cfd4b53SBruce M Simpson break; 1812265ed012SBruce M Simpson #endif 18131e8f5ffaSRobert Watson 1814df8bae1dSRodney W. Grimes case TCP_NODELAY: 1815cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 18168501a69cSRobert Watson INP_WUNLOCK(inp); 1817b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1818df8bae1dSRodney W. Grimes break; 1819df8bae1dSRodney W. Grimes case TCP_MAXSEG: 1820cfe8b629SGarrett Wollman optval = tp->t_maxseg; 18218501a69cSRobert Watson INP_WUNLOCK(inp); 1822b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1823df8bae1dSRodney W. Grimes break; 1824a0292f23SGarrett Wollman case TCP_NOOPT: 1825cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 18268501a69cSRobert Watson INP_WUNLOCK(inp); 1827b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1828a0292f23SGarrett Wollman break; 1829a0292f23SGarrett Wollman case TCP_NOPUSH: 1830cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 18318501a69cSRobert Watson INP_WUNLOCK(inp); 1832b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1833b8af5dfaSRobert Watson break; 1834b8af5dfaSRobert Watson case TCP_INFO: 1835b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 18368501a69cSRobert Watson INP_WUNLOCK(inp); 1837b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 1838a0292f23SGarrett Wollman break; 1839dbc42409SLawrence Stewart case TCP_CONGESTION: 1840af6fef3aSGleb Smirnoff len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 1841dbc42409SLawrence Stewart INP_WUNLOCK(inp); 1842af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, buf, len + 1); 1843dbc42409SLawrence Stewart break; 18442f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 18452f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 18462f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 18472f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 18482f3eb7f4SGleb Smirnoff switch (sopt->sopt_name) { 18492f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 18505a17b6adSMichael Tuexen ui = TP_KEEPIDLE(tp) / hz; 18512f3eb7f4SGleb Smirnoff break; 18522f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 18535a17b6adSMichael Tuexen ui = TP_KEEPINTVL(tp) / hz; 18542f3eb7f4SGleb Smirnoff break; 18552f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 18565a17b6adSMichael Tuexen ui = TP_KEEPINIT(tp) / hz; 18572f3eb7f4SGleb Smirnoff break; 18582f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 18595a17b6adSMichael Tuexen ui = TP_KEEPCNT(tp); 18602f3eb7f4SGleb Smirnoff break; 18612f3eb7f4SGleb Smirnoff } 18622f3eb7f4SGleb Smirnoff INP_WUNLOCK(inp); 18632f3eb7f4SGleb Smirnoff error = sooptcopyout(sopt, &ui, sizeof(ui)); 18642f3eb7f4SGleb Smirnoff break; 186586a996e6SHiren Panchasara #ifdef TCPPCAP 186686a996e6SHiren Panchasara case TCP_PCAP_OUT: 186786a996e6SHiren Panchasara case TCP_PCAP_IN: 186886a996e6SHiren Panchasara optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ? 186986a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts)); 187086a996e6SHiren Panchasara INP_WUNLOCK(inp); 187186a996e6SHiren Panchasara error = sooptcopyout(sopt, &optval, sizeof optval); 187286a996e6SHiren Panchasara break; 187386a996e6SHiren Panchasara #endif 1874281a0fd4SPatrick Kelsey 1875281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 1876281a0fd4SPatrick Kelsey case TCP_FASTOPEN: 1877281a0fd4SPatrick Kelsey optval = tp->t_flags & TF_FASTOPEN; 1878281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 1879281a0fd4SPatrick Kelsey error = sooptcopyout(sopt, &optval, sizeof optval); 1880281a0fd4SPatrick Kelsey break; 1881281a0fd4SPatrick Kelsey #endif 1882df8bae1dSRodney W. Grimes default: 18838501a69cSRobert Watson INP_WUNLOCK(inp); 1884df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1885df8bae1dSRodney W. Grimes break; 1886df8bae1dSRodney W. Grimes } 1887df8bae1dSRodney W. Grimes break; 1888df8bae1dSRodney W. Grimes } 1889df8bae1dSRodney W. Grimes return (error); 1890df8bae1dSRodney W. Grimes } 18918501a69cSRobert Watson #undef INP_WLOCK_RECHECK 1892bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP 1893df8bae1dSRodney W. Grimes 189426e30fbbSDavid Greenman /* 1895df8bae1dSRodney W. Grimes * Attach TCP protocol to socket, allocating 1896df8bae1dSRodney W. Grimes * internet protocol control block, tcp control block, 1897df8bae1dSRodney W. Grimes * bufer space, and entering LISTEN state if to accept connections. 1898df8bae1dSRodney W. Grimes */ 18990312fbe9SPoul-Henning Kamp static int 1900ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so) 1901df8bae1dSRodney W. Grimes { 1902ad3f9ab3SAndre Oppermann struct tcpcb *tp; 1903df8bae1dSRodney W. Grimes struct inpcb *inp; 1904df8bae1dSRodney W. Grimes int error; 1905df8bae1dSRodney W. Grimes 1906df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1907e233e2acSAndre Oppermann error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace); 1908df8bae1dSRodney W. Grimes if (error) 1909df8bae1dSRodney W. Grimes return (error); 1910df8bae1dSRodney W. Grimes } 19116741ecf5SAndre Oppermann so->so_rcv.sb_flags |= SB_AUTOSIZE; 19126741ecf5SAndre Oppermann so->so_snd.sb_flags |= SB_AUTOSIZE; 1913ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 1914603724d3SBjoern A. Zeeb error = in_pcballoc(so, &V_tcbinfo); 1915f2de87feSRobert Watson if (error) { 1916ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1917df8bae1dSRodney W. Grimes return (error); 1918f2de87feSRobert Watson } 1919df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1920fb59c426SYoshinobu Inoue #ifdef INET6 19215cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 1922fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 192363ec505aSMichael Tuexen if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 192463ec505aSMichael Tuexen inp->inp_vflag |= INP_IPV4; 1925fb59c426SYoshinobu Inoue inp->in6p_hops = -1; /* use kernel default */ 1926fb59c426SYoshinobu Inoue } 1927fb59c426SYoshinobu Inoue else 1928fb59c426SYoshinobu Inoue #endif 1929cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1930df8bae1dSRodney W. Grimes tp = tcp_newtcpcb(inp); 1931623dce13SRobert Watson if (tp == NULL) { 1932df8bae1dSRodney W. Grimes in_pcbdetach(inp); 19330206cdb8SBjoern A. Zeeb in_pcbfree(inp); 1934ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1935df8bae1dSRodney W. Grimes return (ENOBUFS); 1936df8bae1dSRodney W. Grimes } 1937df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 19388501a69cSRobert Watson INP_WUNLOCK(inp); 1939ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1940bf840a17SGleb Smirnoff TCPSTATES_INC(TCPS_CLOSED); 1941df8bae1dSRodney W. Grimes return (0); 1942df8bae1dSRodney W. Grimes } 1943df8bae1dSRodney W. Grimes 1944df8bae1dSRodney W. Grimes /* 1945df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 1946df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 1947df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 1948df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 1949df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 1950df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 1951df8bae1dSRodney W. Grimes */ 1952623dce13SRobert Watson static void 1953ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp) 1954df8bae1dSRodney W. Grimes { 1955e6e0b5ffSRobert Watson struct inpcb *inp = tp->t_inpcb; 1956e6e0b5ffSRobert Watson struct socket *so = inp->inp_socket; 1957e6e0b5ffSRobert Watson 1958ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 19598501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1960df8bae1dSRodney W. Grimes 1961623dce13SRobert Watson /* 1962623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 1963623dce13SRobert Watson * socket is still open. 1964623dce13SRobert Watson */ 1965623dce13SRobert Watson if (tp->t_state < TCPS_ESTABLISHED) { 1966df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1967623dce13SRobert Watson KASSERT(tp != NULL, 1968623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 1969623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 1970243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 1971623dce13SRobert Watson KASSERT(tp != NULL, 1972623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 1973623dce13SRobert Watson } else { 1974df8bae1dSRodney W. Grimes soisdisconnecting(so); 1975df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 1976623dce13SRobert Watson tcp_usrclosed(tp); 1977ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 197855bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 1979df8bae1dSRodney W. Grimes } 1980df8bae1dSRodney W. Grimes } 1981df8bae1dSRodney W. Grimes 1982df8bae1dSRodney W. Grimes /* 1983df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 1984df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 1985df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1986df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 1987df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 1988df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 1989df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 1990df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 1991df8bae1dSRodney W. Grimes */ 1992623dce13SRobert Watson static void 1993ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp) 1994df8bae1dSRodney W. Grimes { 1995df8bae1dSRodney W. Grimes 1996ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 19978501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1998e6e0b5ffSRobert Watson 1999df8bae1dSRodney W. Grimes switch (tp->t_state) { 2000df8bae1dSRodney W. Grimes case TCPS_LISTEN: 200109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 200209fe6320SNavdeep Parhar tcp_offload_listen_stop(tp); 200309fe6320SNavdeep Parhar #endif 2004550e9d42SHiren Panchasara tcp_state_change(tp, TCPS_CLOSED); 2005bc65987aSKip Macy /* FALLTHROUGH */ 2006bc65987aSKip Macy case TCPS_CLOSED: 2007df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2008623dce13SRobert Watson /* 2009623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 2010623dce13SRobert Watson * still open. 2011623dce13SRobert Watson */ 2012623dce13SRobert Watson KASSERT(tp != NULL, 2013623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 2014df8bae1dSRodney W. Grimes break; 2015df8bae1dSRodney W. Grimes 2016a0292f23SGarrett Wollman case TCPS_SYN_SENT: 2017df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2018a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 2019a0292f23SGarrett Wollman break; 2020a0292f23SGarrett Wollman 2021df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 202257f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2023df8bae1dSRodney W. Grimes break; 2024df8bae1dSRodney W. Grimes 2025df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 202657f60867SMark Johnston tcp_state_change(tp, TCPS_LAST_ACK); 2027df8bae1dSRodney W. Grimes break; 2028df8bae1dSRodney W. Grimes } 2029abc7d910SRobert Watson if (tp->t_state >= TCPS_FIN_WAIT_2) { 2030df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 2031abc7d910SRobert Watson /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 20327c72af87SMohan Srinivasan if (tp->t_state == TCPS_FIN_WAIT_2) { 20337c72af87SMohan Srinivasan int timeout; 20347c72af87SMohan Srinivasan 20357c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 20369077f387SGleb Smirnoff tcp_finwait2_timeout : TP_MAXIDLE(tp); 2037b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 2038b6239c4aSAndras Olah } 2039df8bae1dSRodney W. Grimes } 20407c72af87SMohan Srinivasan } 2041497057eeSRobert Watson 2042497057eeSRobert Watson #ifdef DDB 2043497057eeSRobert Watson static void 2044497057eeSRobert Watson db_print_indent(int indent) 2045497057eeSRobert Watson { 2046497057eeSRobert Watson int i; 2047497057eeSRobert Watson 2048497057eeSRobert Watson for (i = 0; i < indent; i++) 2049497057eeSRobert Watson db_printf(" "); 2050497057eeSRobert Watson } 2051497057eeSRobert Watson 2052497057eeSRobert Watson static void 2053497057eeSRobert Watson db_print_tstate(int t_state) 2054497057eeSRobert Watson { 2055497057eeSRobert Watson 2056497057eeSRobert Watson switch (t_state) { 2057497057eeSRobert Watson case TCPS_CLOSED: 2058497057eeSRobert Watson db_printf("TCPS_CLOSED"); 2059497057eeSRobert Watson return; 2060497057eeSRobert Watson 2061497057eeSRobert Watson case TCPS_LISTEN: 2062497057eeSRobert Watson db_printf("TCPS_LISTEN"); 2063497057eeSRobert Watson return; 2064497057eeSRobert Watson 2065497057eeSRobert Watson case TCPS_SYN_SENT: 2066497057eeSRobert Watson db_printf("TCPS_SYN_SENT"); 2067497057eeSRobert Watson return; 2068497057eeSRobert Watson 2069497057eeSRobert Watson case TCPS_SYN_RECEIVED: 2070497057eeSRobert Watson db_printf("TCPS_SYN_RECEIVED"); 2071497057eeSRobert Watson return; 2072497057eeSRobert Watson 2073497057eeSRobert Watson case TCPS_ESTABLISHED: 2074497057eeSRobert Watson db_printf("TCPS_ESTABLISHED"); 2075497057eeSRobert Watson return; 2076497057eeSRobert Watson 2077497057eeSRobert Watson case TCPS_CLOSE_WAIT: 2078497057eeSRobert Watson db_printf("TCPS_CLOSE_WAIT"); 2079497057eeSRobert Watson return; 2080497057eeSRobert Watson 2081497057eeSRobert Watson case TCPS_FIN_WAIT_1: 2082497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_1"); 2083497057eeSRobert Watson return; 2084497057eeSRobert Watson 2085497057eeSRobert Watson case TCPS_CLOSING: 2086497057eeSRobert Watson db_printf("TCPS_CLOSING"); 2087497057eeSRobert Watson return; 2088497057eeSRobert Watson 2089497057eeSRobert Watson case TCPS_LAST_ACK: 2090497057eeSRobert Watson db_printf("TCPS_LAST_ACK"); 2091497057eeSRobert Watson return; 2092497057eeSRobert Watson 2093497057eeSRobert Watson case TCPS_FIN_WAIT_2: 2094497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_2"); 2095497057eeSRobert Watson return; 2096497057eeSRobert Watson 2097497057eeSRobert Watson case TCPS_TIME_WAIT: 2098497057eeSRobert Watson db_printf("TCPS_TIME_WAIT"); 2099497057eeSRobert Watson return; 2100497057eeSRobert Watson 2101497057eeSRobert Watson default: 2102497057eeSRobert Watson db_printf("unknown"); 2103497057eeSRobert Watson return; 2104497057eeSRobert Watson } 2105497057eeSRobert Watson } 2106497057eeSRobert Watson 2107497057eeSRobert Watson static void 2108497057eeSRobert Watson db_print_tflags(u_int t_flags) 2109497057eeSRobert Watson { 2110497057eeSRobert Watson int comma; 2111497057eeSRobert Watson 2112497057eeSRobert Watson comma = 0; 2113497057eeSRobert Watson if (t_flags & TF_ACKNOW) { 2114497057eeSRobert Watson db_printf("%sTF_ACKNOW", comma ? ", " : ""); 2115497057eeSRobert Watson comma = 1; 2116497057eeSRobert Watson } 2117497057eeSRobert Watson if (t_flags & TF_DELACK) { 2118497057eeSRobert Watson db_printf("%sTF_DELACK", comma ? ", " : ""); 2119497057eeSRobert Watson comma = 1; 2120497057eeSRobert Watson } 2121497057eeSRobert Watson if (t_flags & TF_NODELAY) { 2122497057eeSRobert Watson db_printf("%sTF_NODELAY", comma ? ", " : ""); 2123497057eeSRobert Watson comma = 1; 2124497057eeSRobert Watson } 2125497057eeSRobert Watson if (t_flags & TF_NOOPT) { 2126497057eeSRobert Watson db_printf("%sTF_NOOPT", comma ? ", " : ""); 2127497057eeSRobert Watson comma = 1; 2128497057eeSRobert Watson } 2129497057eeSRobert Watson if (t_flags & TF_SENTFIN) { 2130497057eeSRobert Watson db_printf("%sTF_SENTFIN", comma ? ", " : ""); 2131497057eeSRobert Watson comma = 1; 2132497057eeSRobert Watson } 2133497057eeSRobert Watson if (t_flags & TF_REQ_SCALE) { 2134497057eeSRobert Watson db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 2135497057eeSRobert Watson comma = 1; 2136497057eeSRobert Watson } 2137497057eeSRobert Watson if (t_flags & TF_RCVD_SCALE) { 2138497057eeSRobert Watson db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 2139497057eeSRobert Watson comma = 1; 2140497057eeSRobert Watson } 2141497057eeSRobert Watson if (t_flags & TF_REQ_TSTMP) { 2142497057eeSRobert Watson db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 2143497057eeSRobert Watson comma = 1; 2144497057eeSRobert Watson } 2145497057eeSRobert Watson if (t_flags & TF_RCVD_TSTMP) { 2146497057eeSRobert Watson db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 2147497057eeSRobert Watson comma = 1; 2148497057eeSRobert Watson } 2149497057eeSRobert Watson if (t_flags & TF_SACK_PERMIT) { 2150497057eeSRobert Watson db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 2151497057eeSRobert Watson comma = 1; 2152497057eeSRobert Watson } 2153497057eeSRobert Watson if (t_flags & TF_NEEDSYN) { 2154497057eeSRobert Watson db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 2155497057eeSRobert Watson comma = 1; 2156497057eeSRobert Watson } 2157497057eeSRobert Watson if (t_flags & TF_NEEDFIN) { 2158497057eeSRobert Watson db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 2159497057eeSRobert Watson comma = 1; 2160497057eeSRobert Watson } 2161497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 2162497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 2163497057eeSRobert Watson comma = 1; 2164497057eeSRobert Watson } 2165497057eeSRobert Watson if (t_flags & TF_MORETOCOME) { 2166497057eeSRobert Watson db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 2167497057eeSRobert Watson comma = 1; 2168497057eeSRobert Watson } 2169497057eeSRobert Watson if (t_flags & TF_LQ_OVERFLOW) { 2170497057eeSRobert Watson db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 2171497057eeSRobert Watson comma = 1; 2172497057eeSRobert Watson } 2173497057eeSRobert Watson if (t_flags & TF_LASTIDLE) { 2174497057eeSRobert Watson db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 2175497057eeSRobert Watson comma = 1; 2176497057eeSRobert Watson } 2177497057eeSRobert Watson if (t_flags & TF_RXWIN0SENT) { 2178497057eeSRobert Watson db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 2179497057eeSRobert Watson comma = 1; 2180497057eeSRobert Watson } 2181497057eeSRobert Watson if (t_flags & TF_FASTRECOVERY) { 2182497057eeSRobert Watson db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 2183497057eeSRobert Watson comma = 1; 2184497057eeSRobert Watson } 2185dbc42409SLawrence Stewart if (t_flags & TF_CONGRECOVERY) { 2186dbc42409SLawrence Stewart db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 2187dbc42409SLawrence Stewart comma = 1; 2188dbc42409SLawrence Stewart } 2189497057eeSRobert Watson if (t_flags & TF_WASFRECOVERY) { 2190497057eeSRobert Watson db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 2191497057eeSRobert Watson comma = 1; 2192497057eeSRobert Watson } 2193497057eeSRobert Watson if (t_flags & TF_SIGNATURE) { 2194497057eeSRobert Watson db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 2195497057eeSRobert Watson comma = 1; 2196497057eeSRobert Watson } 2197497057eeSRobert Watson if (t_flags & TF_FORCEDATA) { 2198497057eeSRobert Watson db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 2199497057eeSRobert Watson comma = 1; 2200497057eeSRobert Watson } 2201497057eeSRobert Watson if (t_flags & TF_TSO) { 2202497057eeSRobert Watson db_printf("%sTF_TSO", comma ? ", " : ""); 2203497057eeSRobert Watson comma = 1; 2204497057eeSRobert Watson } 2205f2512ba1SRui Paulo if (t_flags & TF_ECN_PERMIT) { 2206f2512ba1SRui Paulo db_printf("%sTF_ECN_PERMIT", comma ? ", " : ""); 2207f2512ba1SRui Paulo comma = 1; 2208f2512ba1SRui Paulo } 2209281a0fd4SPatrick Kelsey if (t_flags & TF_FASTOPEN) { 2210281a0fd4SPatrick Kelsey db_printf("%sTF_FASTOPEN", comma ? ", " : ""); 2211281a0fd4SPatrick Kelsey comma = 1; 2212281a0fd4SPatrick Kelsey } 2213497057eeSRobert Watson } 2214497057eeSRobert Watson 2215497057eeSRobert Watson static void 2216497057eeSRobert Watson db_print_toobflags(char t_oobflags) 2217497057eeSRobert Watson { 2218497057eeSRobert Watson int comma; 2219497057eeSRobert Watson 2220497057eeSRobert Watson comma = 0; 2221497057eeSRobert Watson if (t_oobflags & TCPOOB_HAVEDATA) { 2222497057eeSRobert Watson db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 2223497057eeSRobert Watson comma = 1; 2224497057eeSRobert Watson } 2225497057eeSRobert Watson if (t_oobflags & TCPOOB_HADDATA) { 2226497057eeSRobert Watson db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 2227497057eeSRobert Watson comma = 1; 2228497057eeSRobert Watson } 2229497057eeSRobert Watson } 2230497057eeSRobert Watson 2231497057eeSRobert Watson static void 2232497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 2233497057eeSRobert Watson { 2234497057eeSRobert Watson 2235497057eeSRobert Watson db_print_indent(indent); 2236497057eeSRobert Watson db_printf("%s at %p\n", name, tp); 2237497057eeSRobert Watson 2238497057eeSRobert Watson indent += 2; 2239497057eeSRobert Watson 2240497057eeSRobert Watson db_print_indent(indent); 2241497057eeSRobert Watson db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 22424741bfcbSPatrick Kelsey LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 2243497057eeSRobert Watson 2244497057eeSRobert Watson db_print_indent(indent); 224585d94372SRobert Watson db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 2246e2f2059fSMike Silbersack &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 2247497057eeSRobert Watson 2248497057eeSRobert Watson db_print_indent(indent); 2249e2f2059fSMike Silbersack db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 2250e2f2059fSMike Silbersack &tp->t_timers->tt_delack, tp->t_inpcb); 2251497057eeSRobert Watson 2252497057eeSRobert Watson db_print_indent(indent); 2253497057eeSRobert Watson db_printf("t_state: %d (", tp->t_state); 2254497057eeSRobert Watson db_print_tstate(tp->t_state); 2255497057eeSRobert Watson db_printf(")\n"); 2256497057eeSRobert Watson 2257497057eeSRobert Watson db_print_indent(indent); 2258497057eeSRobert Watson db_printf("t_flags: 0x%x (", tp->t_flags); 2259497057eeSRobert Watson db_print_tflags(tp->t_flags); 2260497057eeSRobert Watson db_printf(")\n"); 2261497057eeSRobert Watson 2262497057eeSRobert Watson db_print_indent(indent); 2263497057eeSRobert Watson db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 2264497057eeSRobert Watson tp->snd_una, tp->snd_max, tp->snd_nxt); 2265497057eeSRobert Watson 2266497057eeSRobert Watson db_print_indent(indent); 2267497057eeSRobert Watson db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 2268497057eeSRobert Watson tp->snd_up, tp->snd_wl1, tp->snd_wl2); 2269497057eeSRobert Watson 2270497057eeSRobert Watson db_print_indent(indent); 2271497057eeSRobert Watson db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 2272497057eeSRobert Watson tp->iss, tp->irs, tp->rcv_nxt); 2273497057eeSRobert Watson 2274497057eeSRobert Watson db_print_indent(indent); 22753ac12506SJonathan T. Looney db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n", 2276497057eeSRobert Watson tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 2277497057eeSRobert Watson 2278497057eeSRobert Watson db_print_indent(indent); 22793ac12506SJonathan T. Looney db_printf("snd_wnd: %u snd_cwnd: %u\n", 22801c18314dSAndre Oppermann tp->snd_wnd, tp->snd_cwnd); 2281497057eeSRobert Watson 2282497057eeSRobert Watson db_print_indent(indent); 22833ac12506SJonathan T. Looney db_printf("snd_ssthresh: %u snd_recover: " 22841c18314dSAndre Oppermann "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 2285497057eeSRobert Watson 2286497057eeSRobert Watson db_print_indent(indent); 22870c39d38dSGleb Smirnoff db_printf("t_rcvtime: %u t_startime: %u\n", 22880c39d38dSGleb Smirnoff tp->t_rcvtime, tp->t_starttime); 2289497057eeSRobert Watson 2290497057eeSRobert Watson db_print_indent(indent); 22911c18314dSAndre Oppermann db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 22921c18314dSAndre Oppermann tp->t_rtttime, tp->t_rtseq); 2293497057eeSRobert Watson 2294497057eeSRobert Watson db_print_indent(indent); 22951c18314dSAndre Oppermann db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 22961c18314dSAndre Oppermann tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 2297497057eeSRobert Watson 2298497057eeSRobert Watson db_print_indent(indent); 2299497057eeSRobert Watson db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 2300497057eeSRobert Watson "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 2301497057eeSRobert Watson tp->t_rttbest); 2302497057eeSRobert Watson 2303497057eeSRobert Watson db_print_indent(indent); 23043ac12506SJonathan T. Looney db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n", 2305497057eeSRobert Watson tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 2306497057eeSRobert Watson 2307497057eeSRobert Watson db_print_indent(indent); 2308497057eeSRobert Watson db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 2309497057eeSRobert Watson db_print_toobflags(tp->t_oobflags); 2310497057eeSRobert Watson db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 2311497057eeSRobert Watson 2312497057eeSRobert Watson db_print_indent(indent); 2313497057eeSRobert Watson db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 2314497057eeSRobert Watson tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 2315497057eeSRobert Watson 2316497057eeSRobert Watson db_print_indent(indent); 23179f78a87aSJohn Baldwin db_printf("ts_recent: %u ts_recent_age: %u\n", 23181a553740SAndre Oppermann tp->ts_recent, tp->ts_recent_age); 2319497057eeSRobert Watson 2320497057eeSRobert Watson db_print_indent(indent); 2321497057eeSRobert Watson db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 23223ac12506SJonathan T. Looney "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 2323497057eeSRobert Watson 2324497057eeSRobert Watson db_print_indent(indent); 23253ac12506SJonathan T. Looney db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x " 23269f78a87aSJohn Baldwin "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 2327497057eeSRobert Watson tp->snd_recover_prev, tp->t_badrxtwin); 2328497057eeSRobert Watson 2329497057eeSRobert Watson db_print_indent(indent); 23303529149eSAndre Oppermann db_printf("snd_numholes: %d snd_holes first: %p\n", 23313529149eSAndre Oppermann tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 2332497057eeSRobert Watson 2333497057eeSRobert Watson db_print_indent(indent); 2334497057eeSRobert Watson db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: " 2335497057eeSRobert Watson "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata); 2336497057eeSRobert Watson 2337497057eeSRobert Watson /* Skip sackblks, sackhint. */ 2338497057eeSRobert Watson 2339497057eeSRobert Watson db_print_indent(indent); 2340497057eeSRobert Watson db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 2341497057eeSRobert Watson tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 2342497057eeSRobert Watson } 2343497057eeSRobert Watson 2344497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 2345497057eeSRobert Watson { 2346497057eeSRobert Watson struct tcpcb *tp; 2347497057eeSRobert Watson 2348497057eeSRobert Watson if (!have_addr) { 2349497057eeSRobert Watson db_printf("usage: show tcpcb <addr>\n"); 2350497057eeSRobert Watson return; 2351497057eeSRobert Watson } 2352497057eeSRobert Watson tp = (struct tcpcb *)addr; 2353497057eeSRobert Watson 2354497057eeSRobert Watson db_print_tcpcb(tp, "tcpcb", 0); 2355497057eeSRobert Watson } 2356497057eeSRobert Watson #endif 2357