1c398230bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1993 5623dce13SRobert Watson * The Regents of the University of California. 6497057eeSRobert Watson * Copyright (c) 2006-2007 Robert N. M. Watson 7fa046d87SRobert Watson * Copyright (c) 2010-2011 Juniper Networks, Inc. 8623dce13SRobert Watson * All rights reserved. 9df8bae1dSRodney W. Grimes * 10fa046d87SRobert Watson * Portions of this software were developed by Robert N. M. Watson under 11fa046d87SRobert Watson * contract to Juniper Networks, Inc. 12fa046d87SRobert Watson * 13df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 14df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 15df8bae1dSRodney W. Grimes * are met: 16df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 17df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 18df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 19df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 20df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 21fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 22df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 23df8bae1dSRodney W. Grimes * without specific prior written permission. 24df8bae1dSRodney W. Grimes * 25df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35df8bae1dSRodney W. Grimes * SUCH DAMAGE. 36df8bae1dSRodney W. Grimes * 371fdbc7aeSGarrett Wollman * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 38df8bae1dSRodney W. Grimes */ 39df8bae1dSRodney W. Grimes 404b421e2dSMike Silbersack #include <sys/cdefs.h> 414b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 424b421e2dSMike Silbersack 43497057eeSRobert Watson #include "opt_ddb.h" 441cfd4b53SBruce M Simpson #include "opt_inet.h" 45fb59c426SYoshinobu Inoue #include "opt_inet6.h" 46fcf59617SAndrey V. Elsukov #include "opt_ipsec.h" 47b2e60773SJohn Baldwin #include "opt_kern_tls.h" 480cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 490cc12cc5SJoerg Wunsch 50df8bae1dSRodney W. Grimes #include <sys/param.h> 51df8bae1dSRodney W. Grimes #include <sys/systm.h> 529077f387SGleb Smirnoff #include <sys/limits.h> 53f76fcf6dSJeffrey Hsu #include <sys/malloc.h> 5455bceb1eSRandall Stewart #include <sys/refcount.h> 55c7a82f90SGarrett Wollman #include <sys/kernel.h> 56b2e60773SJohn Baldwin #include <sys/ktls.h> 5798163b98SPoul-Henning Kamp #include <sys/sysctl.h> 58df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 59fb59c426SYoshinobu Inoue #ifdef INET6 60fb59c426SYoshinobu Inoue #include <sys/domain.h> 61fb59c426SYoshinobu Inoue #endif /* INET6 */ 62df8bae1dSRodney W. Grimes #include <sys/socket.h> 63df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 64df8bae1dSRodney W. Grimes #include <sys/protosw.h> 6591421ba2SRobert Watson #include <sys/proc.h> 6691421ba2SRobert Watson #include <sys/jail.h> 67f5cf1e5fSJulien Charbon #include <sys/syslog.h> 68df8bae1dSRodney W. Grimes 69497057eeSRobert Watson #ifdef DDB 70497057eeSRobert Watson #include <ddb/ddb.h> 71497057eeSRobert Watson #endif 72497057eeSRobert Watson 73df8bae1dSRodney W. Grimes #include <net/if.h> 7476039bc8SGleb Smirnoff #include <net/if_var.h> 75df8bae1dSRodney W. Grimes #include <net/route.h> 76530c0060SRobert Watson #include <net/vnet.h> 77df8bae1dSRodney W. Grimes 78df8bae1dSRodney W. Grimes #include <netinet/in.h> 795d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h> 80df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 81b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h> 82b5e8ce9fSBruce Evans #include <netinet/in_var.h> 83df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 84fb59c426SYoshinobu Inoue #ifdef INET6 85b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h> 86b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 87fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 88a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h> 89fb59c426SYoshinobu Inoue #endif 902de3e790SGleb Smirnoff #include <netinet/tcp.h> 91df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 92df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 93df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 94df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 952529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h> 96df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 974644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 98c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h> 99fd389e7cSRandall Stewart #include <netinet/tcp_hpts.h> 10086a996e6SHiren Panchasara #ifdef TCPPCAP 10186a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 10286a996e6SHiren Panchasara #endif 103610ee2f9SDavid Greenman #ifdef TCPDEBUG 104df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 105610ee2f9SDavid Greenman #endif 10609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 107bc65987aSKip Macy #include <netinet/tcp_offload.h> 10809fe6320SNavdeep Parhar #endif 109fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 110df8bae1dSRodney W. Grimes 111df8bae1dSRodney W. Grimes /* 112df8bae1dSRodney W. Grimes * TCP protocol interface to socket abstraction. 113df8bae1dSRodney W. Grimes */ 11456dc72c3SPawel Jakub Dawidek static int tcp_attach(struct socket *); 115b287c6c7SBjoern A. Zeeb #ifdef INET 1164d77a549SAlfred Perlstein static int tcp_connect(struct tcpcb *, struct sockaddr *, 1174d77a549SAlfred Perlstein struct thread *td); 118b287c6c7SBjoern A. Zeeb #endif /* INET */ 119fb59c426SYoshinobu Inoue #ifdef INET6 1204d77a549SAlfred Perlstein static int tcp6_connect(struct tcpcb *, struct sockaddr *, 1214d77a549SAlfred Perlstein struct thread *td); 122fb59c426SYoshinobu Inoue #endif /* INET6 */ 123623dce13SRobert Watson static void tcp_disconnect(struct tcpcb *); 124623dce13SRobert Watson static void tcp_usrclosed(struct tcpcb *); 125b8af5dfaSRobert Watson static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 1262c37256eSGarrett Wollman 1272c37256eSGarrett Wollman #ifdef TCPDEBUG 1281db24ffbSJonathan Lemon #define TCPDEBUG0 int ostate = 0 1292c37256eSGarrett Wollman #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 1304cc20ab1SSeigo Tanimura #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 1314cc20ab1SSeigo Tanimura tcp_trace(TA_USER, ostate, tp, 0, 0, req) 1322c37256eSGarrett Wollman #else 1332c37256eSGarrett Wollman #define TCPDEBUG0 1342c37256eSGarrett Wollman #define TCPDEBUG1() 1352c37256eSGarrett Wollman #define TCPDEBUG2(req) 1362c37256eSGarrett Wollman #endif 1372c37256eSGarrett Wollman 1382c37256eSGarrett Wollman /* 1392c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 1402c37256eSGarrett Wollman * and an internet control block. 1412c37256eSGarrett Wollman */ 1422c37256eSGarrett Wollman static int 143b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td) 1442c37256eSGarrett Wollman { 145f76fcf6dSJeffrey Hsu struct inpcb *inp; 146623dce13SRobert Watson struct tcpcb *tp = NULL; 147623dce13SRobert Watson int error; 1482c37256eSGarrett Wollman TCPDEBUG0; 1492c37256eSGarrett Wollman 150623dce13SRobert Watson inp = sotoinpcb(so); 151623dce13SRobert Watson KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 1522c37256eSGarrett Wollman TCPDEBUG1(); 1532c37256eSGarrett Wollman 15456dc72c3SPawel Jakub Dawidek error = tcp_attach(so); 1552c37256eSGarrett Wollman if (error) 1562c37256eSGarrett Wollman goto out; 1572c37256eSGarrett Wollman 1582c37256eSGarrett Wollman if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1593879597fSAndrey A. Chernov so->so_linger = TCP_LINGERTIME; 160f76fcf6dSJeffrey Hsu 161f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 162f76fcf6dSJeffrey Hsu tp = intotcpcb(inp); 1632c37256eSGarrett Wollman out: 1642c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 1655d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ATTACH); 1662c37256eSGarrett Wollman return error; 1672c37256eSGarrett Wollman } 1682c37256eSGarrett Wollman 1692c37256eSGarrett Wollman /* 170a152f8a3SRobert Watson * tcp_detach is called when the socket layer loses its final reference 171a152f8a3SRobert Watson * to the socket, be it a file descriptor reference, a reference from TCP, 172a152f8a3SRobert Watson * etc. At this point, there is only one case in which we will keep around 173a152f8a3SRobert Watson * inpcb state: time wait. 174c78cbc7bSRobert Watson * 175a152f8a3SRobert Watson * This function can probably be re-absorbed back into tcp_usr_detach() now 176a152f8a3SRobert Watson * that there is a single detach path. 1772c37256eSGarrett Wollman */ 178bc725eafSRobert Watson static void 179c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp) 1802c37256eSGarrett Wollman { 1812c37256eSGarrett Wollman struct tcpcb *tp; 1822c37256eSGarrett Wollman 183079672cbSJulien Charbon INP_INFO_LOCK_ASSERT(&V_tcbinfo); 1848501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 185623dce13SRobert Watson 186c78cbc7bSRobert Watson KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp")); 187c78cbc7bSRobert Watson KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so")); 188953b5606SRobert Watson 189a152f8a3SRobert Watson tp = intotcpcb(inp); 190a152f8a3SRobert Watson 191ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 192623dce13SRobert Watson /* 193a152f8a3SRobert Watson * There are two cases to handle: one in which the time wait 194a152f8a3SRobert Watson * state is being discarded (INP_DROPPED), and one in which 195a152f8a3SRobert Watson * this connection will remain in timewait. In the former, 196a152f8a3SRobert Watson * it is time to discard all state (except tcptw, which has 197a152f8a3SRobert Watson * already been discarded by the timewait close code, which 198a152f8a3SRobert Watson * should be further up the call stack somewhere). In the 199a152f8a3SRobert Watson * latter case, we detach from the socket, but leave the pcb 200a152f8a3SRobert Watson * present until timewait ends. 201623dce13SRobert Watson * 202a152f8a3SRobert Watson * XXXRW: Would it be cleaner to free the tcptw here? 203cea40c48SJulien Charbon * 204cea40c48SJulien Charbon * Astute question indeed, from twtcp perspective there are 205dd388cfdSGleb Smirnoff * four cases to consider: 206cea40c48SJulien Charbon * 207cea40c48SJulien Charbon * #1 tcp_detach is called at tcptw creation time by 208cea40c48SJulien Charbon * tcp_twstart, then do not discard the newly created tcptw 209cea40c48SJulien Charbon * and leave inpcb present until timewait ends 210dd388cfdSGleb Smirnoff * #2 tcp_detach is called at tcptw creation time by 211dd388cfdSGleb Smirnoff * tcp_twstart, but connection is local and tw will be 212dd388cfdSGleb Smirnoff * discarded immediately 213dd388cfdSGleb Smirnoff * #3 tcp_detach is called at timewait end (or reuse) by 214cea40c48SJulien Charbon * tcp_twclose, then the tcptw has already been discarded 215ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 216dd388cfdSGleb Smirnoff * #4 tcp_detach is called() after timewait ends (or reuse) 217cea40c48SJulien Charbon * (e.g. by soclose), then tcptw has already been discarded 218ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 219cea40c48SJulien Charbon * 220cea40c48SJulien Charbon * In all three cases the tcptw should not be freed here. 221623dce13SRobert Watson */ 222ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED) { 223623dce13SRobert Watson in_pcbdetach(inp); 224f5cf1e5fSJulien Charbon if (__predict_true(tp == NULL)) { 2250206cdb8SBjoern A. Zeeb in_pcbfree(inp); 2260206cdb8SBjoern A. Zeeb } else { 227f5cf1e5fSJulien Charbon /* 228f5cf1e5fSJulien Charbon * This case should not happen as in TIMEWAIT 229f5cf1e5fSJulien Charbon * state the inp should not be destroyed before 230f5cf1e5fSJulien Charbon * its tcptw. If INVARIANTS is defined, panic. 231f5cf1e5fSJulien Charbon */ 232f5cf1e5fSJulien Charbon #ifdef INVARIANTS 233f5cf1e5fSJulien Charbon panic("%s: Panic before an inp double-free: " 234f5cf1e5fSJulien Charbon "INP_TIMEWAIT && INP_DROPPED && tp != NULL" 235f5cf1e5fSJulien Charbon , __func__); 236f5cf1e5fSJulien Charbon #else 237f5cf1e5fSJulien Charbon log(LOG_ERR, "%s: Avoid an inp double-free: " 238f5cf1e5fSJulien Charbon "INP_TIMEWAIT && INP_DROPPED && tp != NULL" 239f5cf1e5fSJulien Charbon , __func__); 240f5cf1e5fSJulien Charbon #endif 241f5cf1e5fSJulien Charbon INP_WUNLOCK(inp); 242f5cf1e5fSJulien Charbon } 243f5cf1e5fSJulien Charbon } else { 244623dce13SRobert Watson in_pcbdetach(inp); 2458501a69cSRobert Watson INP_WUNLOCK(inp); 246623dce13SRobert Watson } 247623dce13SRobert Watson } else { 248e6e65783SRobert Watson /* 249a152f8a3SRobert Watson * If the connection is not in timewait, we consider two 250a152f8a3SRobert Watson * two conditions: one in which no further processing is 251a152f8a3SRobert Watson * necessary (dropped || embryonic), and one in which TCP is 252a152f8a3SRobert Watson * not yet done, but no longer requires the socket, so the 253a152f8a3SRobert Watson * pcb will persist for the time being. 254a152f8a3SRobert Watson * 255a152f8a3SRobert Watson * XXXRW: Does the second case still occur? 256e6e65783SRobert Watson */ 257ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED || 258623dce13SRobert Watson tp->t_state < TCPS_SYN_SENT) { 259623dce13SRobert Watson tcp_discardcb(tp); 260623dce13SRobert Watson in_pcbdetach(inp); 2610206cdb8SBjoern A. Zeeb in_pcbfree(inp); 262db3cee51SNavdeep Parhar } else { 263a152f8a3SRobert Watson in_pcbdetach(inp); 264db3cee51SNavdeep Parhar INP_WUNLOCK(inp); 265db3cee51SNavdeep Parhar } 266623dce13SRobert Watson } 267623dce13SRobert Watson } 268c78cbc7bSRobert Watson 269c78cbc7bSRobert Watson /* 270c78cbc7bSRobert Watson * pru_detach() detaches the TCP protocol from the socket. 271c78cbc7bSRobert Watson * If the protocol state is non-embryonic, then can't 272c78cbc7bSRobert Watson * do this directly: have to initiate a pru_disconnect(), 273c78cbc7bSRobert Watson * which may finish later; embryonic TCB's can just 274c78cbc7bSRobert Watson * be discarded here. 275c78cbc7bSRobert Watson */ 276c78cbc7bSRobert Watson static void 277c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so) 278c78cbc7bSRobert Watson { 279c78cbc7bSRobert Watson struct inpcb *inp; 280079672cbSJulien Charbon int rlock = 0; 2816573d758SMatt Macy struct epoch_tracker et; 282c78cbc7bSRobert Watson 283c78cbc7bSRobert Watson inp = sotoinpcb(so); 284c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL")); 285079672cbSJulien Charbon if (!INP_INFO_WLOCKED(&V_tcbinfo)) { 2866573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, et); 287079672cbSJulien Charbon rlock = 1; 288079672cbSJulien Charbon } 2898501a69cSRobert Watson INP_WLOCK(inp); 290c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 291c78cbc7bSRobert Watson ("tcp_usr_detach: inp_socket == NULL")); 292c78cbc7bSRobert Watson tcp_detach(so, inp); 293079672cbSJulien Charbon if (rlock) 2946573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 2952c37256eSGarrett Wollman } 2962c37256eSGarrett Wollman 297b287c6c7SBjoern A. Zeeb #ifdef INET 2982c37256eSGarrett Wollman /* 2992c37256eSGarrett Wollman * Give the socket an address. 3002c37256eSGarrett Wollman */ 3012c37256eSGarrett Wollman static int 302b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 3032c37256eSGarrett Wollman { 3042c37256eSGarrett Wollman int error = 0; 305f76fcf6dSJeffrey Hsu struct inpcb *inp; 306623dce13SRobert Watson struct tcpcb *tp = NULL; 3072c37256eSGarrett Wollman struct sockaddr_in *sinp; 3082c37256eSGarrett Wollman 30952710de1SPawel Jakub Dawidek sinp = (struct sockaddr_in *)nam; 31052710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sinp)) 31152710de1SPawel Jakub Dawidek return (EINVAL); 3122c37256eSGarrett Wollman /* 3132c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 3142c37256eSGarrett Wollman * to them. 3152c37256eSGarrett Wollman */ 3162c37256eSGarrett Wollman if (sinp->sin_family == AF_INET && 31752710de1SPawel Jakub Dawidek IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 31852710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 31952710de1SPawel Jakub Dawidek 320623dce13SRobert Watson TCPDEBUG0; 321623dce13SRobert Watson inp = sotoinpcb(so); 322623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 3238501a69cSRobert Watson INP_WLOCK(inp); 324ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 325623dce13SRobert Watson error = EINVAL; 3262c37256eSGarrett Wollman goto out; 327623dce13SRobert Watson } 328623dce13SRobert Watson tp = intotcpcb(inp); 329623dce13SRobert Watson TCPDEBUG1(); 330fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 331623dce13SRobert Watson error = in_pcbbind(inp, nam, td->td_ucred); 332fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 333623dce13SRobert Watson out: 334623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 3355d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 3368501a69cSRobert Watson INP_WUNLOCK(inp); 337623dce13SRobert Watson 338623dce13SRobert Watson return (error); 3392c37256eSGarrett Wollman } 340b287c6c7SBjoern A. Zeeb #endif /* INET */ 3412c37256eSGarrett Wollman 342fb59c426SYoshinobu Inoue #ifdef INET6 343fb59c426SYoshinobu Inoue static int 344b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 345fb59c426SYoshinobu Inoue { 346fb59c426SYoshinobu Inoue int error = 0; 347f76fcf6dSJeffrey Hsu struct inpcb *inp; 348623dce13SRobert Watson struct tcpcb *tp = NULL; 3490ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 350*4a91aa8fSMichael Tuexen u_char vflagsav; 351fb59c426SYoshinobu Inoue 3520ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 3530ecd976eSBjoern A. Zeeb if (nam->sa_len != sizeof (*sin6)) 35452710de1SPawel Jakub Dawidek return (EINVAL); 355fb59c426SYoshinobu Inoue /* 356fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 357fb59c426SYoshinobu Inoue * to them. 358fb59c426SYoshinobu Inoue */ 3590ecd976eSBjoern A. Zeeb if (sin6->sin6_family == AF_INET6 && 3600ecd976eSBjoern A. Zeeb IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 36152710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 36252710de1SPawel Jakub Dawidek 363623dce13SRobert Watson TCPDEBUG0; 364623dce13SRobert Watson inp = sotoinpcb(so); 365623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 3668501a69cSRobert Watson INP_WLOCK(inp); 367*4a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 368ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 369623dce13SRobert Watson error = EINVAL; 370623dce13SRobert Watson goto out; 371623dce13SRobert Watson } 372623dce13SRobert Watson tp = intotcpcb(inp); 373623dce13SRobert Watson TCPDEBUG1(); 374fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 375fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 376fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 377b287c6c7SBjoern A. Zeeb #ifdef INET 37866ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 3790ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 380fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 3810ecd976eSBjoern A. Zeeb else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 382fb59c426SYoshinobu Inoue struct sockaddr_in sin; 383fb59c426SYoshinobu Inoue 3840ecd976eSBjoern A. Zeeb in6_sin6_2_sin(&sin, sin6); 385888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 386888973f5SMichael Tuexen error = EAFNOSUPPORT; 387888973f5SMichael Tuexen INP_HASH_WUNLOCK(&V_tcbinfo); 388888973f5SMichael Tuexen goto out; 389888973f5SMichael Tuexen } 390fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 391fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 392b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)&sin, 393b0330ed9SPawel Jakub Dawidek td->td_ucred); 394fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 395fb59c426SYoshinobu Inoue goto out; 396fb59c426SYoshinobu Inoue } 397fb59c426SYoshinobu Inoue } 398b287c6c7SBjoern A. Zeeb #endif 399b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, nam, td->td_ucred); 400fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 401623dce13SRobert Watson out: 402*4a91aa8fSMichael Tuexen if (error != 0) 403*4a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 404623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 4055d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 4068501a69cSRobert Watson INP_WUNLOCK(inp); 407623dce13SRobert Watson return (error); 408fb59c426SYoshinobu Inoue } 409fb59c426SYoshinobu Inoue #endif /* INET6 */ 410fb59c426SYoshinobu Inoue 411b287c6c7SBjoern A. Zeeb #ifdef INET 4122c37256eSGarrett Wollman /* 4132c37256eSGarrett Wollman * Prepare to accept connections. 4142c37256eSGarrett Wollman */ 4152c37256eSGarrett Wollman static int 416d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 4172c37256eSGarrett Wollman { 4182c37256eSGarrett Wollman int error = 0; 419f76fcf6dSJeffrey Hsu struct inpcb *inp; 420623dce13SRobert Watson struct tcpcb *tp = NULL; 4212c37256eSGarrett Wollman 422623dce13SRobert Watson TCPDEBUG0; 423623dce13SRobert Watson inp = sotoinpcb(so); 424623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 4258501a69cSRobert Watson INP_WLOCK(inp); 426ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 427623dce13SRobert Watson error = EINVAL; 428623dce13SRobert Watson goto out; 429623dce13SRobert Watson } 430623dce13SRobert Watson tp = intotcpcb(inp); 431623dce13SRobert Watson TCPDEBUG1(); 4320daccb9cSRobert Watson SOCK_LOCK(so); 4330daccb9cSRobert Watson error = solisten_proto_check(so); 434fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 4350daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) 436b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 437fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 4380daccb9cSRobert Watson if (error == 0) { 43957f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 440d374e81eSRobert Watson solisten_proto(so, backlog); 44109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 44237cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 44309fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 44409fe6320SNavdeep Parhar #endif 4450daccb9cSRobert Watson } 4460daccb9cSRobert Watson SOCK_UNLOCK(so); 447623dce13SRobert Watson 44868bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) 449281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 45018a75309SPatrick Kelsey 451623dce13SRobert Watson out: 452623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 4535d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 4548501a69cSRobert Watson INP_WUNLOCK(inp); 455623dce13SRobert Watson return (error); 4562c37256eSGarrett Wollman } 457b287c6c7SBjoern A. Zeeb #endif /* INET */ 4582c37256eSGarrett Wollman 459fb59c426SYoshinobu Inoue #ifdef INET6 460fb59c426SYoshinobu Inoue static int 461d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 462fb59c426SYoshinobu Inoue { 463fb59c426SYoshinobu Inoue int error = 0; 464f76fcf6dSJeffrey Hsu struct inpcb *inp; 465623dce13SRobert Watson struct tcpcb *tp = NULL; 466*4a91aa8fSMichael Tuexen u_char vflagsav; 467fb59c426SYoshinobu Inoue 468623dce13SRobert Watson TCPDEBUG0; 469623dce13SRobert Watson inp = sotoinpcb(so); 470623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 4718501a69cSRobert Watson INP_WLOCK(inp); 472ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 473623dce13SRobert Watson error = EINVAL; 474623dce13SRobert Watson goto out; 475623dce13SRobert Watson } 476*4a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 477623dce13SRobert Watson tp = intotcpcb(inp); 478623dce13SRobert Watson TCPDEBUG1(); 4790daccb9cSRobert Watson SOCK_LOCK(so); 4800daccb9cSRobert Watson error = solisten_proto_check(so); 481fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 4820daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) { 483fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 48466ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 485fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 486b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 487fb59c426SYoshinobu Inoue } 488fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 4890daccb9cSRobert Watson if (error == 0) { 49057f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 491d374e81eSRobert Watson solisten_proto(so, backlog); 49209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 49337cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 49409fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 49509fe6320SNavdeep Parhar #endif 4960daccb9cSRobert Watson } 4970daccb9cSRobert Watson SOCK_UNLOCK(so); 498623dce13SRobert Watson 49968bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) 500281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 50118a75309SPatrick Kelsey 502*4a91aa8fSMichael Tuexen if (error != 0) 503*4a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 504*4a91aa8fSMichael Tuexen 505623dce13SRobert Watson out: 506623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 5075d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 5088501a69cSRobert Watson INP_WUNLOCK(inp); 509623dce13SRobert Watson return (error); 510fb59c426SYoshinobu Inoue } 511fb59c426SYoshinobu Inoue #endif /* INET6 */ 512fb59c426SYoshinobu Inoue 513b287c6c7SBjoern A. Zeeb #ifdef INET 5142c37256eSGarrett Wollman /* 5152c37256eSGarrett Wollman * Initiate connection to peer. 5162c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 5172c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 5182c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 5192c37256eSGarrett Wollman * Send initial segment on connection. 5202c37256eSGarrett Wollman */ 5212c37256eSGarrett Wollman static int 522b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 5232c37256eSGarrett Wollman { 5242c37256eSGarrett Wollman int error = 0; 525f76fcf6dSJeffrey Hsu struct inpcb *inp; 526623dce13SRobert Watson struct tcpcb *tp = NULL; 5272c37256eSGarrett Wollman struct sockaddr_in *sinp; 5282c37256eSGarrett Wollman 52957bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 530e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sinp)) 531e29ef13fSDon Lewis return (EINVAL); 53252710de1SPawel Jakub Dawidek /* 53352710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 53452710de1SPawel Jakub Dawidek */ 5352c37256eSGarrett Wollman if (sinp->sin_family == AF_INET 53652710de1SPawel Jakub Dawidek && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 53752710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 538b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 539b89e82ddSJamie Gritton return (error); 54075c13541SPoul-Henning Kamp 541623dce13SRobert Watson TCPDEBUG0; 542623dce13SRobert Watson inp = sotoinpcb(so); 543623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 5448501a69cSRobert Watson INP_WLOCK(inp); 545eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 546eb96dc33SJulien Charbon error = EADDRINUSE; 547eb96dc33SJulien Charbon goto out; 548eb96dc33SJulien Charbon } 549eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 550eb96dc33SJulien Charbon error = ECONNREFUSED; 551623dce13SRobert Watson goto out; 552623dce13SRobert Watson } 553623dce13SRobert Watson tp = intotcpcb(inp); 554623dce13SRobert Watson TCPDEBUG1(); 555b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, td)) != 0) 5562c37256eSGarrett Wollman goto out; 55709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 55809fe6320SNavdeep Parhar if (registered_toedevs > 0 && 55937cc0ecbSNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 56009fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 56109fe6320SNavdeep Parhar goto out; 56209fe6320SNavdeep Parhar #endif 56309fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 56455bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 565623dce13SRobert Watson out: 566623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 567e79cb051SGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 5688501a69cSRobert Watson INP_WUNLOCK(inp); 569623dce13SRobert Watson return (error); 5702c37256eSGarrett Wollman } 571b287c6c7SBjoern A. Zeeb #endif /* INET */ 5722c37256eSGarrett Wollman 573fb59c426SYoshinobu Inoue #ifdef INET6 574fb59c426SYoshinobu Inoue static int 575b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 576fb59c426SYoshinobu Inoue { 577fb59c426SYoshinobu Inoue int error = 0; 578f76fcf6dSJeffrey Hsu struct inpcb *inp; 579623dce13SRobert Watson struct tcpcb *tp = NULL; 5800ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 581*4a91aa8fSMichael Tuexen u_int8_t incflagsav; 582*4a91aa8fSMichael Tuexen u_char vflagsav; 583623dce13SRobert Watson 584623dce13SRobert Watson TCPDEBUG0; 585fb59c426SYoshinobu Inoue 5860ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 5870ecd976eSBjoern A. Zeeb if (nam->sa_len != sizeof (*sin6)) 588e29ef13fSDon Lewis return (EINVAL); 58952710de1SPawel Jakub Dawidek /* 59052710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 59152710de1SPawel Jakub Dawidek */ 5920ecd976eSBjoern A. Zeeb if (sin6->sin6_family == AF_INET6 5930ecd976eSBjoern A. Zeeb && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 59452710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 595fb59c426SYoshinobu Inoue 596623dce13SRobert Watson inp = sotoinpcb(so); 597623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 5988501a69cSRobert Watson INP_WLOCK(inp); 599*4a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 600*4a91aa8fSMichael Tuexen incflagsav = inp->inp_inc.inc_flags; 601eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 602eb96dc33SJulien Charbon error = EADDRINUSE; 603eb96dc33SJulien Charbon goto out; 604eb96dc33SJulien Charbon } 605eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 606eb96dc33SJulien Charbon error = ECONNREFUSED; 607623dce13SRobert Watson goto out; 608623dce13SRobert Watson } 609623dce13SRobert Watson tp = intotcpcb(inp); 610623dce13SRobert Watson TCPDEBUG1(); 611b287c6c7SBjoern A. Zeeb #ifdef INET 612fa046d87SRobert Watson /* 613fa046d87SRobert Watson * XXXRW: Some confusion: V4/V6 flags relate to binding, and 614fa046d87SRobert Watson * therefore probably require the hash lock, which isn't held here. 615fa046d87SRobert Watson * Is this a significant problem? 616fa046d87SRobert Watson */ 6170ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 618fb59c426SYoshinobu Inoue struct sockaddr_in sin; 619fb59c426SYoshinobu Inoue 620d46a5312SMaxim Konovalov if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 621d46a5312SMaxim Konovalov error = EINVAL; 622d46a5312SMaxim Konovalov goto out; 623d46a5312SMaxim Konovalov } 6245dba6adaSMichael Tuexen if ((inp->inp_vflag & INP_IPV4) == 0) { 6255dba6adaSMichael Tuexen error = EAFNOSUPPORT; 6265dba6adaSMichael Tuexen goto out; 6275dba6adaSMichael Tuexen } 62833841545SHajimu UMEMOTO 6290ecd976eSBjoern A. Zeeb in6_sin6_2_sin(&sin, sin6); 630888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 631888973f5SMichael Tuexen error = EAFNOSUPPORT; 632888973f5SMichael Tuexen goto out; 633888973f5SMichael Tuexen } 634b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, 635b89e82ddSJamie Gritton &sin.sin_addr)) != 0) 636413628a7SBjoern A. Zeeb goto out; 637*4a91aa8fSMichael Tuexen inp->inp_vflag |= INP_IPV4; 638*4a91aa8fSMichael Tuexen inp->inp_vflag &= ~INP_IPV6; 639b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 640fb59c426SYoshinobu Inoue goto out; 64109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 64209fe6320SNavdeep Parhar if (registered_toedevs > 0 && 643adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 64409fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 64509fe6320SNavdeep Parhar goto out; 64609fe6320SNavdeep Parhar #endif 64755bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 648fb59c426SYoshinobu Inoue goto out; 6495dba6adaSMichael Tuexen } else { 6505dba6adaSMichael Tuexen if ((inp->inp_vflag & INP_IPV6) == 0) { 6515dba6adaSMichael Tuexen error = EAFNOSUPPORT; 6525dba6adaSMichael Tuexen goto out; 6535dba6adaSMichael Tuexen } 654fb59c426SYoshinobu Inoue } 655b287c6c7SBjoern A. Zeeb #endif 656*4a91aa8fSMichael Tuexen if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0) 657*4a91aa8fSMichael Tuexen goto out; 658fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 659fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 660dcdb4371SBjoern A. Zeeb inp->inp_inc.inc_flags |= INC_ISIPV6; 661b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, td)) != 0) 662fb59c426SYoshinobu Inoue goto out; 66309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 66409fe6320SNavdeep Parhar if (registered_toedevs > 0 && 665adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 66609fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 66709fe6320SNavdeep Parhar goto out; 66809fe6320SNavdeep Parhar #endif 66909fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 67055bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 671623dce13SRobert Watson 672623dce13SRobert Watson out: 673*4a91aa8fSMichael Tuexen /* 674*4a91aa8fSMichael Tuexen * If the implicit bind in the connect call fails, restore 675*4a91aa8fSMichael Tuexen * the flags we modified. 676*4a91aa8fSMichael Tuexen */ 677*4a91aa8fSMichael Tuexen if (error != 0 && inp->inp_lport == 0) { 678*4a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 679*4a91aa8fSMichael Tuexen inp->inp_inc.inc_flags = incflagsav; 680*4a91aa8fSMichael Tuexen } 681*4a91aa8fSMichael Tuexen 682623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 6835d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 6848501a69cSRobert Watson INP_WUNLOCK(inp); 685623dce13SRobert Watson return (error); 686fb59c426SYoshinobu Inoue } 687fb59c426SYoshinobu Inoue #endif /* INET6 */ 688fb59c426SYoshinobu Inoue 6892c37256eSGarrett Wollman /* 6902c37256eSGarrett Wollman * Initiate disconnect from peer. 6912c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 6922c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 6932c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 6942c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 6952c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 6962c37256eSGarrett Wollman * when peer sends FIN and acks ours. 6972c37256eSGarrett Wollman * 6982c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 6992c37256eSGarrett Wollman */ 7002c37256eSGarrett Wollman static int 7012c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 7022c37256eSGarrett Wollman { 703f76fcf6dSJeffrey Hsu struct inpcb *inp; 704623dce13SRobert Watson struct tcpcb *tp = NULL; 7056573d758SMatt Macy struct epoch_tracker et; 706623dce13SRobert Watson int error = 0; 7072c37256eSGarrett Wollman 708623dce13SRobert Watson TCPDEBUG0; 7096573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, et); 710623dce13SRobert Watson inp = sotoinpcb(so); 711623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 7128501a69cSRobert Watson INP_WLOCK(inp); 713489dcc92SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) 714489dcc92SJulien Charbon goto out; 715489dcc92SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 71621367f63SSam Leffler error = ECONNRESET; 717623dce13SRobert Watson goto out; 718623dce13SRobert Watson } 719623dce13SRobert Watson tp = intotcpcb(inp); 720623dce13SRobert Watson TCPDEBUG1(); 721623dce13SRobert Watson tcp_disconnect(tp); 722623dce13SRobert Watson out: 723623dce13SRobert Watson TCPDEBUG2(PRU_DISCONNECT); 7245d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_DISCONNECT); 7258501a69cSRobert Watson INP_WUNLOCK(inp); 7266573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 727623dce13SRobert Watson return (error); 7282c37256eSGarrett Wollman } 7292c37256eSGarrett Wollman 730b287c6c7SBjoern A. Zeeb #ifdef INET 7312c37256eSGarrett Wollman /* 7328296cddfSRobert Watson * Accept a connection. Essentially all the work is done at higher levels; 7338296cddfSRobert Watson * just return the address of the peer, storing through addr. 7342c37256eSGarrett Wollman */ 7352c37256eSGarrett Wollman static int 73657bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 7372c37256eSGarrett Wollman { 7382c37256eSGarrett Wollman int error = 0; 739f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 7401db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 74126ef6ac4SDon Lewis struct in_addr addr; 74226ef6ac4SDon Lewis in_port_t port = 0; 7431db24ffbSJonathan Lemon TCPDEBUG0; 7442c37256eSGarrett Wollman 7453d2d3ef4SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 7463d2d3ef4SRobert Watson return (ECONNABORTED); 747f76fcf6dSJeffrey Hsu 748f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 749623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 7508501a69cSRobert Watson INP_WLOCK(inp); 751ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 7523d2d3ef4SRobert Watson error = ECONNABORTED; 753623dce13SRobert Watson goto out; 754623dce13SRobert Watson } 7551db24ffbSJonathan Lemon tp = intotcpcb(inp); 7561db24ffbSJonathan Lemon TCPDEBUG1(); 757f76fcf6dSJeffrey Hsu 758f76fcf6dSJeffrey Hsu /* 75954d642bbSRobert Watson * We inline in_getpeeraddr and COMMON_END here, so that we can 76026ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 76126ef6ac4SDon Lewis * release the lock. 762f76fcf6dSJeffrey Hsu */ 76326ef6ac4SDon Lewis port = inp->inp_fport; 76426ef6ac4SDon Lewis addr = inp->inp_faddr; 765f76fcf6dSJeffrey Hsu 766623dce13SRobert Watson out: 767623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 7685d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 7698501a69cSRobert Watson INP_WUNLOCK(inp); 77026ef6ac4SDon Lewis if (error == 0) 77126ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 77226ef6ac4SDon Lewis return error; 7732c37256eSGarrett Wollman } 774b287c6c7SBjoern A. Zeeb #endif /* INET */ 7752c37256eSGarrett Wollman 776fb59c426SYoshinobu Inoue #ifdef INET6 777fb59c426SYoshinobu Inoue static int 778fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 779fb59c426SYoshinobu Inoue { 780f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 781fb59c426SYoshinobu Inoue int error = 0; 7821db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 78326ef6ac4SDon Lewis struct in_addr addr; 78426ef6ac4SDon Lewis struct in6_addr addr6; 7856573d758SMatt Macy struct epoch_tracker et; 78626ef6ac4SDon Lewis in_port_t port = 0; 78726ef6ac4SDon Lewis int v4 = 0; 7881db24ffbSJonathan Lemon TCPDEBUG0; 789fb59c426SYoshinobu Inoue 790b4470c16SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 791b4470c16SRobert Watson return (ECONNABORTED); 792f76fcf6dSJeffrey Hsu 793f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 794623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 7956573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, et); 7968501a69cSRobert Watson INP_WLOCK(inp); 797ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 79821367f63SSam Leffler error = ECONNABORTED; 799623dce13SRobert Watson goto out; 800623dce13SRobert Watson } 8011db24ffbSJonathan Lemon tp = intotcpcb(inp); 8021db24ffbSJonathan Lemon TCPDEBUG1(); 803623dce13SRobert Watson 80426ef6ac4SDon Lewis /* 80526ef6ac4SDon Lewis * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 80626ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 80726ef6ac4SDon Lewis * release the lock. 80826ef6ac4SDon Lewis */ 80926ef6ac4SDon Lewis if (inp->inp_vflag & INP_IPV4) { 81026ef6ac4SDon Lewis v4 = 1; 81126ef6ac4SDon Lewis port = inp->inp_fport; 81226ef6ac4SDon Lewis addr = inp->inp_faddr; 81326ef6ac4SDon Lewis } else { 81426ef6ac4SDon Lewis port = inp->inp_fport; 81526ef6ac4SDon Lewis addr6 = inp->in6p_faddr; 81626ef6ac4SDon Lewis } 81726ef6ac4SDon Lewis 818623dce13SRobert Watson out: 819623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 8205d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 8218501a69cSRobert Watson INP_WUNLOCK(inp); 8226573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 82326ef6ac4SDon Lewis if (error == 0) { 82426ef6ac4SDon Lewis if (v4) 82526ef6ac4SDon Lewis *nam = in6_v4mapsin6_sockaddr(port, &addr); 82626ef6ac4SDon Lewis else 82726ef6ac4SDon Lewis *nam = in6_sockaddr(port, &addr6); 82826ef6ac4SDon Lewis } 82926ef6ac4SDon Lewis return error; 830fb59c426SYoshinobu Inoue } 831fb59c426SYoshinobu Inoue #endif /* INET6 */ 832f76fcf6dSJeffrey Hsu 833f76fcf6dSJeffrey Hsu /* 8342c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 8352c37256eSGarrett Wollman */ 8362c37256eSGarrett Wollman static int 8372c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 8382c37256eSGarrett Wollman { 8392c37256eSGarrett Wollman int error = 0; 840f76fcf6dSJeffrey Hsu struct inpcb *inp; 841623dce13SRobert Watson struct tcpcb *tp = NULL; 8426573d758SMatt Macy struct epoch_tracker et; 8432c37256eSGarrett Wollman 844623dce13SRobert Watson TCPDEBUG0; 8456573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, et); 846623dce13SRobert Watson inp = sotoinpcb(so); 847623dce13SRobert Watson KASSERT(inp != NULL, ("inp == NULL")); 8488501a69cSRobert Watson INP_WLOCK(inp); 849ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 85021367f63SSam Leffler error = ECONNRESET; 851623dce13SRobert Watson goto out; 852623dce13SRobert Watson } 853623dce13SRobert Watson tp = intotcpcb(inp); 854623dce13SRobert Watson TCPDEBUG1(); 8552c37256eSGarrett Wollman socantsendmore(so); 856623dce13SRobert Watson tcp_usrclosed(tp); 857ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 85855bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 859623dce13SRobert Watson 860623dce13SRobert Watson out: 861623dce13SRobert Watson TCPDEBUG2(PRU_SHUTDOWN); 8625d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN); 8638501a69cSRobert Watson INP_WUNLOCK(inp); 8646573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 865623dce13SRobert Watson 866623dce13SRobert Watson return (error); 8672c37256eSGarrett Wollman } 8682c37256eSGarrett Wollman 8692c37256eSGarrett Wollman /* 8702c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 8712c37256eSGarrett Wollman */ 8722c37256eSGarrett Wollman static int 8732c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 8742c37256eSGarrett Wollman { 875f76fcf6dSJeffrey Hsu struct inpcb *inp; 876623dce13SRobert Watson struct tcpcb *tp = NULL; 877623dce13SRobert Watson int error = 0; 8782c37256eSGarrett Wollman 879623dce13SRobert Watson TCPDEBUG0; 880623dce13SRobert Watson inp = sotoinpcb(so); 881623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 8828501a69cSRobert Watson INP_WLOCK(inp); 883ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 88421367f63SSam Leffler error = ECONNRESET; 885623dce13SRobert Watson goto out; 886623dce13SRobert Watson } 887623dce13SRobert Watson tp = intotcpcb(inp); 888623dce13SRobert Watson TCPDEBUG1(); 889281a0fd4SPatrick Kelsey /* 890281a0fd4SPatrick Kelsey * For passively-created TFO connections, don't attempt a window 891281a0fd4SPatrick Kelsey * update while still in SYN_RECEIVED as this may trigger an early 892281a0fd4SPatrick Kelsey * SYN|ACK. It is preferable to have the SYN|ACK be sent along with 893281a0fd4SPatrick Kelsey * application response data, or failing that, when the DELACK timer 894281a0fd4SPatrick Kelsey * expires. 895281a0fd4SPatrick Kelsey */ 89668bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 897281a0fd4SPatrick Kelsey (tp->t_state == TCPS_SYN_RECEIVED)) 898281a0fd4SPatrick Kelsey goto out; 89909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 90009fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 90109fe6320SNavdeep Parhar tcp_offload_rcvd(tp); 902460cf046SNavdeep Parhar else 90309fe6320SNavdeep Parhar #endif 90455bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 905623dce13SRobert Watson 906623dce13SRobert Watson out: 907623dce13SRobert Watson TCPDEBUG2(PRU_RCVD); 9085d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVD); 9098501a69cSRobert Watson INP_WUNLOCK(inp); 910623dce13SRobert Watson return (error); 9112c37256eSGarrett Wollman } 9122c37256eSGarrett Wollman 9132c37256eSGarrett Wollman /* 9142c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 9159c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 9169c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 9179c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 9189c9906e9SPeter Wemm * generally are caller-frees. 9192c37256eSGarrett Wollman */ 9202c37256eSGarrett Wollman static int 92157bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 922b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 9232c37256eSGarrett Wollman { 9242c37256eSGarrett Wollman int error = 0; 925f76fcf6dSJeffrey Hsu struct inpcb *inp; 926623dce13SRobert Watson struct tcpcb *tp = NULL; 9276573d758SMatt Macy struct epoch_tracker net_et; 928888973f5SMichael Tuexen #ifdef INET 92951e08d53SMichael Tuexen #ifdef INET6 93051e08d53SMichael Tuexen struct sockaddr_in sin; 93151e08d53SMichael Tuexen #endif 93251e08d53SMichael Tuexen struct sockaddr_in *sinp; 933888973f5SMichael Tuexen #endif 934fb59c426SYoshinobu Inoue #ifdef INET6 935fb59c426SYoshinobu Inoue int isipv6; 936fb59c426SYoshinobu Inoue #endif 937*4a91aa8fSMichael Tuexen u_int8_t incflagsav; 938*4a91aa8fSMichael Tuexen u_char vflagsav; 939*4a91aa8fSMichael Tuexen bool restoreflags; 9409c9906e9SPeter Wemm TCPDEBUG0; 9412c37256eSGarrett Wollman 942f76fcf6dSJeffrey Hsu /* 943fa046d87SRobert Watson * We require the pcbinfo lock if we will close the socket as part of 944fa046d87SRobert Watson * this call. 945f76fcf6dSJeffrey Hsu */ 946fa046d87SRobert Watson if (flags & PRUS_EOF) 9476573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, net_et); 948f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 949623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 9508501a69cSRobert Watson INP_WLOCK(inp); 951*4a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 952*4a91aa8fSMichael Tuexen incflagsav = inp->inp_inc.inc_flags; 953*4a91aa8fSMichael Tuexen restoreflags = false; 954ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 9557ff0b850SAndre Oppermann if (control) 9567ff0b850SAndre Oppermann m_freem(control); 9572cbcd3c1SGleb Smirnoff /* 9582cbcd3c1SGleb Smirnoff * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible 9592cbcd3c1SGleb Smirnoff * for freeing memory. 9602cbcd3c1SGleb Smirnoff */ 9612cbcd3c1SGleb Smirnoff if (m && (flags & PRUS_NOTREADY) == 0) 9627ff0b850SAndre Oppermann m_freem(m); 96321367f63SSam Leffler error = ECONNRESET; 9649c9906e9SPeter Wemm goto out; 9659c9906e9SPeter Wemm } 9669c9906e9SPeter Wemm tp = intotcpcb(inp); 9679c9906e9SPeter Wemm TCPDEBUG1(); 968888973f5SMichael Tuexen if (nam != NULL && tp->t_state < TCPS_SYN_SENT) { 969888973f5SMichael Tuexen switch (nam->sa_family) { 970888973f5SMichael Tuexen #ifdef INET 971888973f5SMichael Tuexen case AF_INET: 972888973f5SMichael Tuexen sinp = (struct sockaddr_in *)nam; 973888973f5SMichael Tuexen if (sinp->sin_len != sizeof(struct sockaddr_in)) { 974888973f5SMichael Tuexen if (m) 975888973f5SMichael Tuexen m_freem(m); 976888973f5SMichael Tuexen error = EINVAL; 977888973f5SMichael Tuexen goto out; 978888973f5SMichael Tuexen } 979888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV6) != 0) { 980888973f5SMichael Tuexen if (m) 981888973f5SMichael Tuexen m_freem(m); 982888973f5SMichael Tuexen error = EAFNOSUPPORT; 983888973f5SMichael Tuexen goto out; 984888973f5SMichael Tuexen } 985888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 986888973f5SMichael Tuexen if (m) 987888973f5SMichael Tuexen m_freem(m); 988888973f5SMichael Tuexen error = EAFNOSUPPORT; 989888973f5SMichael Tuexen goto out; 990888973f5SMichael Tuexen } 991888973f5SMichael Tuexen if ((error = prison_remote_ip4(td->td_ucred, 992888973f5SMichael Tuexen &sinp->sin_addr))) { 993888973f5SMichael Tuexen if (m) 994888973f5SMichael Tuexen m_freem(m); 995888973f5SMichael Tuexen goto out; 996888973f5SMichael Tuexen } 997888973f5SMichael Tuexen #ifdef INET6 998888973f5SMichael Tuexen isipv6 = 0; 999888973f5SMichael Tuexen #endif 1000888973f5SMichael Tuexen break; 1001888973f5SMichael Tuexen #endif /* INET */ 1002888973f5SMichael Tuexen #ifdef INET6 1003888973f5SMichael Tuexen case AF_INET6: 1004888973f5SMichael Tuexen { 10050ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 1006888973f5SMichael Tuexen 10070ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 10080ecd976eSBjoern A. Zeeb if (sin6->sin6_len != sizeof(*sin6)) { 1009888973f5SMichael Tuexen if (m) 1010888973f5SMichael Tuexen m_freem(m); 1011888973f5SMichael Tuexen error = EINVAL; 1012888973f5SMichael Tuexen goto out; 1013888973f5SMichael Tuexen } 10140ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 1015888973f5SMichael Tuexen if (m) 1016888973f5SMichael Tuexen m_freem(m); 1017888973f5SMichael Tuexen error = EAFNOSUPPORT; 1018888973f5SMichael Tuexen goto out; 1019888973f5SMichael Tuexen } 10200ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 1021888973f5SMichael Tuexen #ifdef INET 1022888973f5SMichael Tuexen if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 1023888973f5SMichael Tuexen error = EINVAL; 1024888973f5SMichael Tuexen if (m) 1025888973f5SMichael Tuexen m_freem(m); 1026888973f5SMichael Tuexen goto out; 1027888973f5SMichael Tuexen } 1028888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV4) == 0) { 1029888973f5SMichael Tuexen error = EAFNOSUPPORT; 1030888973f5SMichael Tuexen if (m) 1031888973f5SMichael Tuexen m_freem(m); 1032888973f5SMichael Tuexen goto out; 1033888973f5SMichael Tuexen } 1034*4a91aa8fSMichael Tuexen restoreflags = true; 1035888973f5SMichael Tuexen inp->inp_vflag &= ~INP_IPV6; 1036888973f5SMichael Tuexen sinp = &sin; 10370ecd976eSBjoern A. Zeeb in6_sin6_2_sin(sinp, sin6); 1038888973f5SMichael Tuexen if (IN_MULTICAST( 1039888973f5SMichael Tuexen ntohl(sinp->sin_addr.s_addr))) { 1040888973f5SMichael Tuexen error = EAFNOSUPPORT; 1041888973f5SMichael Tuexen if (m) 1042888973f5SMichael Tuexen m_freem(m); 1043888973f5SMichael Tuexen goto out; 1044888973f5SMichael Tuexen } 1045888973f5SMichael Tuexen if ((error = prison_remote_ip4(td->td_ucred, 1046888973f5SMichael Tuexen &sinp->sin_addr))) { 1047888973f5SMichael Tuexen if (m) 1048888973f5SMichael Tuexen m_freem(m); 1049888973f5SMichael Tuexen goto out; 1050888973f5SMichael Tuexen } 1051888973f5SMichael Tuexen isipv6 = 0; 1052888973f5SMichael Tuexen #else /* !INET */ 1053888973f5SMichael Tuexen error = EAFNOSUPPORT; 1054888973f5SMichael Tuexen if (m) 1055888973f5SMichael Tuexen m_freem(m); 1056888973f5SMichael Tuexen goto out; 1057888973f5SMichael Tuexen #endif /* INET */ 1058888973f5SMichael Tuexen } else { 1059888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV6) == 0) { 1060888973f5SMichael Tuexen if (m) 1061888973f5SMichael Tuexen m_freem(m); 1062888973f5SMichael Tuexen error = EAFNOSUPPORT; 1063888973f5SMichael Tuexen goto out; 1064888973f5SMichael Tuexen } 1065*4a91aa8fSMichael Tuexen restoreflags = true; 1066888973f5SMichael Tuexen inp->inp_vflag &= ~INP_IPV4; 1067888973f5SMichael Tuexen inp->inp_inc.inc_flags |= INC_ISIPV6; 1068888973f5SMichael Tuexen if ((error = prison_remote_ip6(td->td_ucred, 10690ecd976eSBjoern A. Zeeb &sin6->sin6_addr))) { 1070888973f5SMichael Tuexen if (m) 1071888973f5SMichael Tuexen m_freem(m); 1072888973f5SMichael Tuexen goto out; 1073888973f5SMichael Tuexen } 1074888973f5SMichael Tuexen isipv6 = 1; 1075888973f5SMichael Tuexen } 1076888973f5SMichael Tuexen break; 1077888973f5SMichael Tuexen } 1078888973f5SMichael Tuexen #endif /* INET6 */ 1079888973f5SMichael Tuexen default: 1080888973f5SMichael Tuexen if (m) 1081888973f5SMichael Tuexen m_freem(m); 1082888973f5SMichael Tuexen error = EAFNOSUPPORT; 1083888973f5SMichael Tuexen goto out; 1084888973f5SMichael Tuexen } 1085888973f5SMichael Tuexen } 10869c9906e9SPeter Wemm if (control) { 10879c9906e9SPeter Wemm /* TCP doesn't do control messages (rights, creds, etc) */ 10889c9906e9SPeter Wemm if (control->m_len) { 10899c9906e9SPeter Wemm m_freem(control); 10902c37256eSGarrett Wollman if (m) 10912c37256eSGarrett Wollman m_freem(m); 1092744f87eaSDavid Greenman error = EINVAL; 1093744f87eaSDavid Greenman goto out; 10942c37256eSGarrett Wollman } 10959c9906e9SPeter Wemm m_freem(control); /* empty control, just free it */ 10969c9906e9SPeter Wemm } 10972c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 1098651e4e6aSGleb Smirnoff sbappendstream(&so->so_snd, m, flags); 10992c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 11002c37256eSGarrett Wollman /* 11012c37256eSGarrett Wollman * Do implied connect if not yet connected, 11022c37256eSGarrett Wollman * initialize window to default value, and 11030c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 11042c37256eSGarrett Wollman */ 1105fb59c426SYoshinobu Inoue #ifdef INET6 1106fb59c426SYoshinobu Inoue if (isipv6) 1107b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 1108fb59c426SYoshinobu Inoue #endif /* INET6 */ 1109b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1110b287c6c7SBjoern A. Zeeb else 1111b287c6c7SBjoern A. Zeeb #endif 1112b287c6c7SBjoern A. Zeeb #ifdef INET 1113888973f5SMichael Tuexen error = tcp_connect(tp, 1114888973f5SMichael Tuexen (struct sockaddr *)sinp, td); 1115b287c6c7SBjoern A. Zeeb #endif 1116*4a91aa8fSMichael Tuexen /* 1117*4a91aa8fSMichael Tuexen * The bind operation in tcp_connect succeeded. We 1118*4a91aa8fSMichael Tuexen * no longer want to restore the flags if later 1119*4a91aa8fSMichael Tuexen * operations fail. 1120*4a91aa8fSMichael Tuexen */ 1121*4a91aa8fSMichael Tuexen if (error == 0 || inp->inp_lport != 0) 1122*4a91aa8fSMichael Tuexen restoreflags = false; 1123*4a91aa8fSMichael Tuexen 11242c37256eSGarrett Wollman if (error) 11252c37256eSGarrett Wollman goto out; 1126c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) 1127c560df6fSPatrick Kelsey tcp_fastopen_connect(tp); 112818a75309SPatrick Kelsey else { 11292c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 11302c37256eSGarrett Wollman tcp_mss(tp, -1); 11312c37256eSGarrett Wollman } 1132c560df6fSPatrick Kelsey } 11332c37256eSGarrett Wollman if (flags & PRUS_EOF) { 11342c37256eSGarrett Wollman /* 11352c37256eSGarrett Wollman * Close the send side of the connection after 11362c37256eSGarrett Wollman * the data is sent. 11372c37256eSGarrett Wollman */ 1138ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 11392c37256eSGarrett Wollman socantsendmore(so); 1140623dce13SRobert Watson tcp_usrclosed(tp); 11412c37256eSGarrett Wollman } 11422cbcd3c1SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED) && 11432cbcd3c1SGleb Smirnoff !(flags & PRUS_NOTREADY)) { 1144b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 1145b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 114655bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 1147b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 1148b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 1149b0acefa8SBill Fenner } 11502c37256eSGarrett Wollman } else { 1151623dce13SRobert Watson /* 1152623dce13SRobert Watson * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 1153623dce13SRobert Watson */ 1154d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 11552c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 1156d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 11572c37256eSGarrett Wollman m_freem(m); 11582c37256eSGarrett Wollman error = ENOBUFS; 11592c37256eSGarrett Wollman goto out; 11602c37256eSGarrett Wollman } 11612c37256eSGarrett Wollman /* 11622c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 11632c37256eSGarrett Wollman * the urgent pointer points to the last octet 11642c37256eSGarrett Wollman * of urgent data. We continue, however, 11652c37256eSGarrett Wollman * to consider it to indicate the first octet 11662c37256eSGarrett Wollman * of data past the urgent section. 11672c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 11682c37256eSGarrett Wollman */ 1169651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_snd, m, flags); 1170d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1171ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 1172ef53690bSGarrett Wollman /* 1173ef53690bSGarrett Wollman * Do implied connect if not yet connected, 1174ef53690bSGarrett Wollman * initialize window to default value, and 11750c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 1176ef53690bSGarrett Wollman */ 117718a75309SPatrick Kelsey 1178c560df6fSPatrick Kelsey /* 1179c560df6fSPatrick Kelsey * Not going to contemplate SYN|URG 1180c560df6fSPatrick Kelsey */ 1181c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) 1182c560df6fSPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 1183fb59c426SYoshinobu Inoue #ifdef INET6 1184fb59c426SYoshinobu Inoue if (isipv6) 1185b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 1186fb59c426SYoshinobu Inoue #endif /* INET6 */ 1187b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1188b287c6c7SBjoern A. Zeeb else 1189b287c6c7SBjoern A. Zeeb #endif 1190b287c6c7SBjoern A. Zeeb #ifdef INET 1191888973f5SMichael Tuexen error = tcp_connect(tp, 1192888973f5SMichael Tuexen (struct sockaddr *)sinp, td); 1193b287c6c7SBjoern A. Zeeb #endif 1194*4a91aa8fSMichael Tuexen /* 1195*4a91aa8fSMichael Tuexen * The bind operation in tcp_connect succeeded. We 1196*4a91aa8fSMichael Tuexen * no longer want to restore the flags if later 1197*4a91aa8fSMichael Tuexen * operations fail. 1198*4a91aa8fSMichael Tuexen */ 1199*4a91aa8fSMichael Tuexen if (error == 0 || inp->inp_lport != 0) 1200*4a91aa8fSMichael Tuexen restoreflags = false; 1201*4a91aa8fSMichael Tuexen 1202ef53690bSGarrett Wollman if (error) 1203ef53690bSGarrett Wollman goto out; 1204ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 1205ef53690bSGarrett Wollman tcp_mss(tp, -1); 1206623dce13SRobert Watson } 1207300fa232SGleb Smirnoff tp->snd_up = tp->snd_una + sbavail(&so->so_snd); 12082cbcd3c1SGleb Smirnoff if (!(flags & PRUS_NOTREADY)) { 12092cdbfa66SPaul Saab tp->t_flags |= TF_FORCEDATA; 121055bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 12112cdbfa66SPaul Saab tp->t_flags &= ~TF_FORCEDATA; 12122c37256eSGarrett Wollman } 12132cbcd3c1SGleb Smirnoff } 12142529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, NULL, 12152529f56eSJonathan T. Looney &inp->inp_socket->so_rcv, 12162529f56eSJonathan T. Looney &inp->inp_socket->so_snd, 12172529f56eSJonathan T. Looney TCP_LOG_USERSEND, error, 12182529f56eSJonathan T. Looney 0, NULL, false); 1219d1401c90SRobert Watson out: 1220*4a91aa8fSMichael Tuexen /* 1221*4a91aa8fSMichael Tuexen * If the request was unsuccessful and we changed flags, 1222*4a91aa8fSMichael Tuexen * restore the original flags. 1223*4a91aa8fSMichael Tuexen */ 1224*4a91aa8fSMichael Tuexen if (error != 0 && restoreflags) { 1225*4a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 1226*4a91aa8fSMichael Tuexen inp->inp_inc.inc_flags = incflagsav; 1227*4a91aa8fSMichael Tuexen } 1228d1401c90SRobert Watson TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 12292c37256eSGarrett Wollman ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 12305d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB : 12315d06879aSGeorge V. Neville-Neil ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 12328501a69cSRobert Watson INP_WUNLOCK(inp); 1233fa046d87SRobert Watson if (flags & PRUS_EOF) 12346573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, net_et); 123573fddedaSPeter Grehan return (error); 12362c37256eSGarrett Wollman } 12372c37256eSGarrett Wollman 12382cbcd3c1SGleb Smirnoff static int 12392cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count) 12402cbcd3c1SGleb Smirnoff { 12412cbcd3c1SGleb Smirnoff struct inpcb *inp; 12422cbcd3c1SGleb Smirnoff struct tcpcb *tp; 12432cbcd3c1SGleb Smirnoff int error; 12442cbcd3c1SGleb Smirnoff 12452cbcd3c1SGleb Smirnoff inp = sotoinpcb(so); 12462cbcd3c1SGleb Smirnoff INP_WLOCK(inp); 12472cbcd3c1SGleb Smirnoff if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 12482cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 124982334850SJohn Baldwin mb_free_notready(m, count); 12502cbcd3c1SGleb Smirnoff return (ECONNRESET); 12512cbcd3c1SGleb Smirnoff } 12522cbcd3c1SGleb Smirnoff tp = intotcpcb(inp); 12532cbcd3c1SGleb Smirnoff 12542cbcd3c1SGleb Smirnoff SOCKBUF_LOCK(&so->so_snd); 12552cbcd3c1SGleb Smirnoff error = sbready(&so->so_snd, m, count); 12562cbcd3c1SGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 12572cbcd3c1SGleb Smirnoff if (error == 0) 125855bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 12592cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 12602cbcd3c1SGleb Smirnoff 12612cbcd3c1SGleb Smirnoff return (error); 12622cbcd3c1SGleb Smirnoff } 12632cbcd3c1SGleb Smirnoff 12642c37256eSGarrett Wollman /* 1265a152f8a3SRobert Watson * Abort the TCP. Drop the connection abruptly. 12662c37256eSGarrett Wollman */ 1267ac45e92fSRobert Watson static void 12682c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 12692c37256eSGarrett Wollman { 1270f76fcf6dSJeffrey Hsu struct inpcb *inp; 1271a152f8a3SRobert Watson struct tcpcb *tp = NULL; 12726573d758SMatt Macy struct epoch_tracker et; 1273623dce13SRobert Watson TCPDEBUG0; 1274c78cbc7bSRobert Watson 1275ac45e92fSRobert Watson inp = sotoinpcb(so); 1276c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 1277c78cbc7bSRobert Watson 12786573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, et); 12798501a69cSRobert Watson INP_WLOCK(inp); 1280c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 1281c78cbc7bSRobert Watson ("tcp_usr_abort: inp_socket == NULL")); 1282c78cbc7bSRobert Watson 1283c78cbc7bSRobert Watson /* 1284a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, drop. 1285c78cbc7bSRobert Watson */ 1286ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 1287ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 1288c78cbc7bSRobert Watson tp = intotcpcb(inp); 1289a152f8a3SRobert Watson TCPDEBUG1(); 12908fa799bdSJonathan T. Looney tp = tcp_drop(tp, ECONNABORTED); 12918fa799bdSJonathan T. Looney if (tp == NULL) 12928fa799bdSJonathan T. Looney goto dropped; 1293a152f8a3SRobert Watson TCPDEBUG2(PRU_ABORT); 12945d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ABORT); 1295c78cbc7bSRobert Watson } 1296ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1297a152f8a3SRobert Watson SOCK_LOCK(so); 1298a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 1299a152f8a3SRobert Watson SOCK_UNLOCK(so); 1300ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1301a152f8a3SRobert Watson } 13028501a69cSRobert Watson INP_WUNLOCK(inp); 13038fa799bdSJonathan T. Looney dropped: 13046573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1305a152f8a3SRobert Watson } 1306a152f8a3SRobert Watson 1307a152f8a3SRobert Watson /* 1308a152f8a3SRobert Watson * TCP socket is closed. Start friendly disconnect. 1309a152f8a3SRobert Watson */ 1310a152f8a3SRobert Watson static void 1311a152f8a3SRobert Watson tcp_usr_close(struct socket *so) 1312a152f8a3SRobert Watson { 1313a152f8a3SRobert Watson struct inpcb *inp; 1314a152f8a3SRobert Watson struct tcpcb *tp = NULL; 13156573d758SMatt Macy struct epoch_tracker et; 1316a152f8a3SRobert Watson TCPDEBUG0; 1317a152f8a3SRobert Watson 1318a152f8a3SRobert Watson inp = sotoinpcb(so); 1319a152f8a3SRobert Watson KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 1320a152f8a3SRobert Watson 13216573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, et); 13228501a69cSRobert Watson INP_WLOCK(inp); 1323a152f8a3SRobert Watson KASSERT(inp->inp_socket != NULL, 1324a152f8a3SRobert Watson ("tcp_usr_close: inp_socket == NULL")); 1325a152f8a3SRobert Watson 1326a152f8a3SRobert Watson /* 1327a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, initiate 1328a152f8a3SRobert Watson * a disconnect. 1329a152f8a3SRobert Watson */ 1330ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 1331ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 1332a152f8a3SRobert Watson tp = intotcpcb(inp); 1333a152f8a3SRobert Watson TCPDEBUG1(); 1334a152f8a3SRobert Watson tcp_disconnect(tp); 1335a152f8a3SRobert Watson TCPDEBUG2(PRU_CLOSE); 13365d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CLOSE); 1337a152f8a3SRobert Watson } 1338ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1339a152f8a3SRobert Watson SOCK_LOCK(so); 1340a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 1341a152f8a3SRobert Watson SOCK_UNLOCK(so); 1342ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1343a152f8a3SRobert Watson } 13448501a69cSRobert Watson INP_WUNLOCK(inp); 13456573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 13462c37256eSGarrett Wollman } 13472c37256eSGarrett Wollman 13482c37256eSGarrett Wollman /* 13492c37256eSGarrett Wollman * Receive out-of-band data. 13502c37256eSGarrett Wollman */ 13512c37256eSGarrett Wollman static int 13522c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 13532c37256eSGarrett Wollman { 13542c37256eSGarrett Wollman int error = 0; 1355f76fcf6dSJeffrey Hsu struct inpcb *inp; 1356623dce13SRobert Watson struct tcpcb *tp = NULL; 13572c37256eSGarrett Wollman 1358623dce13SRobert Watson TCPDEBUG0; 1359623dce13SRobert Watson inp = sotoinpcb(so); 1360623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 13618501a69cSRobert Watson INP_WLOCK(inp); 1362ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 136321367f63SSam Leffler error = ECONNRESET; 1364623dce13SRobert Watson goto out; 1365623dce13SRobert Watson } 1366623dce13SRobert Watson tp = intotcpcb(inp); 1367623dce13SRobert Watson TCPDEBUG1(); 13682c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 1369c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 13704cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 13714cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 13722c37256eSGarrett Wollman error = EINVAL; 13732c37256eSGarrett Wollman goto out; 13742c37256eSGarrett Wollman } 13752c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 13762c37256eSGarrett Wollman error = EWOULDBLOCK; 13772c37256eSGarrett Wollman goto out; 13782c37256eSGarrett Wollman } 13792c37256eSGarrett Wollman m->m_len = 1; 13802c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 13812c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 13822c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1383623dce13SRobert Watson 1384623dce13SRobert Watson out: 1385623dce13SRobert Watson TCPDEBUG2(PRU_RCVOOB); 13865d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVOOB); 13878501a69cSRobert Watson INP_WUNLOCK(inp); 1388623dce13SRobert Watson return (error); 13892c37256eSGarrett Wollman } 13902c37256eSGarrett Wollman 1391b287c6c7SBjoern A. Zeeb #ifdef INET 13922c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = { 1393756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1394756d52a1SPoul-Henning Kamp .pru_accept = tcp_usr_accept, 1395756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1396756d52a1SPoul-Henning Kamp .pru_bind = tcp_usr_bind, 1397756d52a1SPoul-Henning Kamp .pru_connect = tcp_usr_connect, 1398756d52a1SPoul-Henning Kamp .pru_control = in_control, 1399756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1400756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1401756d52a1SPoul-Henning Kamp .pru_listen = tcp_usr_listen, 140254d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1403756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1404756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1405756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 14062cbcd3c1SGleb Smirnoff .pru_ready = tcp_usr_ready, 1407756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 140854d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1409a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1410a152f8a3SRobert Watson .pru_close = tcp_usr_close, 14112c37256eSGarrett Wollman }; 1412b287c6c7SBjoern A. Zeeb #endif /* INET */ 1413df8bae1dSRodney W. Grimes 1414fb59c426SYoshinobu Inoue #ifdef INET6 1415fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = { 1416756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1417756d52a1SPoul-Henning Kamp .pru_accept = tcp6_usr_accept, 1418756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1419756d52a1SPoul-Henning Kamp .pru_bind = tcp6_usr_bind, 1420756d52a1SPoul-Henning Kamp .pru_connect = tcp6_usr_connect, 1421756d52a1SPoul-Henning Kamp .pru_control = in6_control, 1422756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1423756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1424756d52a1SPoul-Henning Kamp .pru_listen = tcp6_usr_listen, 1425756d52a1SPoul-Henning Kamp .pru_peeraddr = in6_mapped_peeraddr, 1426756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1427756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1428756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 14292cbcd3c1SGleb Smirnoff .pru_ready = tcp_usr_ready, 1430756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 1431756d52a1SPoul-Henning Kamp .pru_sockaddr = in6_mapped_sockaddr, 1432a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1433a152f8a3SRobert Watson .pru_close = tcp_usr_close, 1434fb59c426SYoshinobu Inoue }; 1435fb59c426SYoshinobu Inoue #endif /* INET6 */ 1436fb59c426SYoshinobu Inoue 1437b287c6c7SBjoern A. Zeeb #ifdef INET 1438a0292f23SGarrett Wollman /* 1439a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 1440a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 14415200e00eSIan Dowse * port number if needed. Call in_pcbconnect_setup to do the routing and 14425200e00eSIan Dowse * to choose a local host address (interface). If there is an existing 14435200e00eSIan Dowse * incarnation of the same connection in TIME-WAIT state and if the remote 14445200e00eSIan Dowse * host was sending CC options and if the connection duration was < MSL, then 1445a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 1446a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 1447a0292f23SGarrett Wollman */ 14480312fbe9SPoul-Henning Kamp static int 1449ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1450a0292f23SGarrett Wollman { 1451a0292f23SGarrett Wollman struct inpcb *inp = tp->t_inpcb, *oinp; 1452a0292f23SGarrett Wollman struct socket *so = inp->inp_socket; 14535200e00eSIan Dowse struct in_addr laddr; 14545200e00eSIan Dowse u_short lport; 1455c3229e05SDavid Greenman int error; 1456a0292f23SGarrett Wollman 14578501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1458fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1459623dce13SRobert Watson 1460a0292f23SGarrett Wollman if (inp->inp_lport == 0) { 14614616026fSErmal Luçi error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 14624616026fSErmal Luçi if (error) 1463fa046d87SRobert Watson goto out; 1464a0292f23SGarrett Wollman } 1465a0292f23SGarrett Wollman 1466a0292f23SGarrett Wollman /* 1467a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 1468a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 1469a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 1470a0292f23SGarrett Wollman */ 14715200e00eSIan Dowse laddr = inp->inp_laddr; 14725200e00eSIan Dowse lport = inp->inp_lport; 14735200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1474b0330ed9SPawel Jakub Dawidek &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 14755200e00eSIan Dowse if (error && oinp == NULL) 1476fa046d87SRobert Watson goto out; 1477fa046d87SRobert Watson if (oinp) { 1478fa046d87SRobert Watson error = EADDRINUSE; 1479fa046d87SRobert Watson goto out; 1480fa046d87SRobert Watson } 14815200e00eSIan Dowse inp->inp_laddr = laddr; 148215bd2b43SDavid Greenman in_pcbrehash(inp); 1483fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1484a0292f23SGarrett Wollman 1485087b55eaSAndre Oppermann /* 1486087b55eaSAndre Oppermann * Compute window scaling to request: 1487087b55eaSAndre Oppermann * Scale to fit into sweet spot. See tcp_syncache.c. 1488087b55eaSAndre Oppermann * XXX: This should move to tcp_output(). 1489087b55eaSAndre Oppermann */ 1490a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 14919b3bc6bfSMike Silbersack (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1492a0292f23SGarrett Wollman tp->request_r_scale++; 1493a0292f23SGarrett Wollman 1494a0292f23SGarrett Wollman soisconnecting(so); 149578b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 149657f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 14978e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 14988e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 14998e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1500a0292f23SGarrett Wollman tcp_sendseqinit(tp); 1501a45d2726SAndras Olah 1502a0292f23SGarrett Wollman return 0; 1503fa046d87SRobert Watson 1504fa046d87SRobert Watson out: 1505fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1506fa046d87SRobert Watson return (error); 1507a0292f23SGarrett Wollman } 1508b287c6c7SBjoern A. Zeeb #endif /* INET */ 1509a0292f23SGarrett Wollman 1510fb59c426SYoshinobu Inoue #ifdef INET6 1511fb59c426SYoshinobu Inoue static int 1512ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1513fb59c426SYoshinobu Inoue { 1514a7e201bbSAndrey V. Elsukov struct inpcb *inp = tp->t_inpcb; 1515fb59c426SYoshinobu Inoue int error; 1516fb59c426SYoshinobu Inoue 15178501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1518fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1519623dce13SRobert Watson 1520fb59c426SYoshinobu Inoue if (inp->inp_lport == 0) { 15214616026fSErmal Luçi error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 15224616026fSErmal Luçi if (error) 1523fa046d87SRobert Watson goto out; 1524fb59c426SYoshinobu Inoue } 1525a7e201bbSAndrey V. Elsukov error = in6_pcbconnect(inp, nam, td->td_ucred); 1526a7e201bbSAndrey V. Elsukov if (error != 0) 1527b598155aSRobert Watson goto out; 1528fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1529fb59c426SYoshinobu Inoue 1530fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1531fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1532970caf60SBjoern A. Zeeb (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1533fb59c426SYoshinobu Inoue tp->request_r_scale++; 1534fb59c426SYoshinobu Inoue 1535a7e201bbSAndrey V. Elsukov soisconnecting(inp->inp_socket); 153678b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 153757f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 15388e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 15398e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 15408e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1541fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1542fb59c426SYoshinobu Inoue 1543fb59c426SYoshinobu Inoue return 0; 1544fa046d87SRobert Watson 1545fa046d87SRobert Watson out: 1546fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1547fa046d87SRobert Watson return error; 1548fb59c426SYoshinobu Inoue } 1549fb59c426SYoshinobu Inoue #endif /* INET6 */ 1550fb59c426SYoshinobu Inoue 1551cfe8b629SGarrett Wollman /* 1552b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1553b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1554b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1555b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1556b8af5dfaSRobert Watson * from Linux. 1557b8af5dfaSRobert Watson */ 1558b8af5dfaSRobert Watson static void 1559ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1560b8af5dfaSRobert Watson { 1561b8af5dfaSRobert Watson 15628501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1563b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1564b8af5dfaSRobert Watson 1565b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1566b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1567b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 15683529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 1569b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1570b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1571b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1572b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1573b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1574b8af5dfaSRobert Watson } 15755a17b6adSMichael Tuexen if (tp->t_flags & TF_ECN_PERMIT) 15765a17b6adSMichael Tuexen ti->tcpi_options |= TCPI_OPT_ECN; 15771baaf834SBruce M Simpson 157843d94734SJohn Baldwin ti->tcpi_rto = tp->t_rxtcur * tick; 15793ac12506SJonathan T. Looney ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick; 15801baaf834SBruce M Simpson ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 15811baaf834SBruce M Simpson ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 15821baaf834SBruce M Simpson 1583b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1584b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1585b8af5dfaSRobert Watson 1586b8af5dfaSRobert Watson /* 1587b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1588b8af5dfaSRobert Watson */ 1589c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1590535fbad6SKip Macy ti->tcpi_rcv_nxt = tp->rcv_nxt; 1591b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 15921c18314dSAndre Oppermann ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1593535fbad6SKip Macy ti->tcpi_snd_nxt = tp->snd_nxt; 159443d94734SJohn Baldwin ti->tcpi_snd_mss = tp->t_maxseg; 159543d94734SJohn Baldwin ti->tcpi_rcv_mss = tp->t_maxseg; 1596f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1597f5d34df5SGeorge V. Neville-Neil ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1598f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1599a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD 1600a6456410SNavdeep Parhar if (tp->t_flags & TF_TOE) { 1601a6456410SNavdeep Parhar ti->tcpi_options |= TCPI_OPT_TOE; 1602a6456410SNavdeep Parhar tcp_offload_tcp_info(tp, ti); 1603a6456410SNavdeep Parhar } 1604a6456410SNavdeep Parhar #endif 1605b8af5dfaSRobert Watson } 1606b8af5dfaSRobert Watson 1607b8af5dfaSRobert Watson /* 16081e8f5ffaSRobert Watson * tcp_ctloutput() must drop the inpcb lock before performing copyin on 16091e8f5ffaSRobert Watson * socket option arguments. When it re-acquires the lock after the copy, it 16101e8f5ffaSRobert Watson * has to revalidate that the connection is still valid for the socket 16111e8f5ffaSRobert Watson * option. 1612cfe8b629SGarrett Wollman */ 1613bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \ 16148501a69cSRobert Watson INP_WLOCK(inp); \ 1615ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 16168501a69cSRobert Watson INP_WUNLOCK(inp); \ 1617bac5bedfSConrad Meyer cleanup; \ 16181e8f5ffaSRobert Watson return (ECONNRESET); \ 16191e8f5ffaSRobert Watson } \ 16201e8f5ffaSRobert Watson tp = intotcpcb(inp); \ 16211e8f5ffaSRobert Watson } while(0) 1622bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */) 16231e8f5ffaSRobert Watson 1624df8bae1dSRodney W. Grimes int 1625ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1626df8bae1dSRodney W. Grimes { 162755bceb1eSRandall Stewart int error; 1628df8bae1dSRodney W. Grimes struct inpcb *inp; 1629cfe8b629SGarrett Wollman struct tcpcb *tp; 163055bceb1eSRandall Stewart struct tcp_function_block *blk; 163155bceb1eSRandall Stewart struct tcp_function_set fsn; 1632df8bae1dSRodney W. Grimes 1633cfe8b629SGarrett Wollman error = 0; 1634df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1635623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1636cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 1637fb59c426SYoshinobu Inoue #ifdef INET6 16385cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 1639fb59c426SYoshinobu Inoue error = ip6_ctloutput(so, sopt); 16405dff1c38SMichael Tuexen /* 16415dff1c38SMichael Tuexen * In case of the IPV6_USE_MIN_MTU socket option, 16425dff1c38SMichael Tuexen * the INC_IPV6MINMTU flag to announce a corresponding 16435dff1c38SMichael Tuexen * MSS during the initial handshake. 16445dff1c38SMichael Tuexen * If the TCP connection is not in the front states, 16455dff1c38SMichael Tuexen * just reduce the MSS being used. 16465dff1c38SMichael Tuexen * This avoids the sending of TCP segments which will 16475dff1c38SMichael Tuexen * be fragmented at the IPv6 layer. 16485dff1c38SMichael Tuexen */ 16495dff1c38SMichael Tuexen if ((error == 0) && 16505dff1c38SMichael Tuexen (sopt->sopt_dir == SOPT_SET) && 16515dff1c38SMichael Tuexen (sopt->sopt_level == IPPROTO_IPV6) && 16525dff1c38SMichael Tuexen (sopt->sopt_name == IPV6_USE_MIN_MTU)) { 16535dff1c38SMichael Tuexen INP_WLOCK(inp); 16545dff1c38SMichael Tuexen if ((inp->inp_flags & 16555dff1c38SMichael Tuexen (INP_TIMEWAIT | INP_DROPPED))) { 16565dff1c38SMichael Tuexen INP_WUNLOCK(inp); 16575dff1c38SMichael Tuexen return (ECONNRESET); 16585dff1c38SMichael Tuexen } 16595dff1c38SMichael Tuexen inp->inp_inc.inc_flags |= INC_IPV6MINMTU; 16605dff1c38SMichael Tuexen tp = intotcpcb(inp); 16615dff1c38SMichael Tuexen if ((tp->t_state >= TCPS_SYN_SENT) && 16625dff1c38SMichael Tuexen (inp->inp_inc.inc_flags & INC_ISIPV6)) { 16635dff1c38SMichael Tuexen struct ip6_pktopts *opt; 16645dff1c38SMichael Tuexen 16655dff1c38SMichael Tuexen opt = inp->in6p_outputopts; 16665dff1c38SMichael Tuexen if ((opt != NULL) && 16675dff1c38SMichael Tuexen (opt->ip6po_minmtu == 16685dff1c38SMichael Tuexen IP6PO_MINMTU_ALL)) { 16695dff1c38SMichael Tuexen if (tp->t_maxseg > TCP6_MSS) { 16705dff1c38SMichael Tuexen tp->t_maxseg = TCP6_MSS; 16715dff1c38SMichael Tuexen } 16725dff1c38SMichael Tuexen } 16735dff1c38SMichael Tuexen } 16745dff1c38SMichael Tuexen INP_WUNLOCK(inp); 16755dff1c38SMichael Tuexen } 1676b287c6c7SBjoern A. Zeeb } 1677fb59c426SYoshinobu Inoue #endif /* INET6 */ 1678b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1679b287c6c7SBjoern A. Zeeb else 1680b287c6c7SBjoern A. Zeeb #endif 1681b287c6c7SBjoern A. Zeeb #ifdef INET 1682b287c6c7SBjoern A. Zeeb { 1683cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 16841e8f5ffaSRobert Watson } 16851e8f5ffaSRobert Watson #endif 1686df8bae1dSRodney W. Grimes return (error); 1687df8bae1dSRodney W. Grimes } 168868cea2b1SJohn Baldwin INP_WLOCK(inp); 1689ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 16908501a69cSRobert Watson INP_WUNLOCK(inp); 16911e8f5ffaSRobert Watson return (ECONNRESET); 1692623dce13SRobert Watson } 169355bceb1eSRandall Stewart tp = intotcpcb(inp); 169455bceb1eSRandall Stewart /* 169555bceb1eSRandall Stewart * Protect the TCP option TCP_FUNCTION_BLK so 169655bceb1eSRandall Stewart * that a sub-function can *never* overwrite this. 169755bceb1eSRandall Stewart */ 169855bceb1eSRandall Stewart if ((sopt->sopt_dir == SOPT_SET) && 169955bceb1eSRandall Stewart (sopt->sopt_name == TCP_FUNCTION_BLK)) { 170055bceb1eSRandall Stewart INP_WUNLOCK(inp); 170155bceb1eSRandall Stewart error = sooptcopyin(sopt, &fsn, sizeof fsn, 170255bceb1eSRandall Stewart sizeof fsn); 170355bceb1eSRandall Stewart if (error) 170455bceb1eSRandall Stewart return (error); 170555bceb1eSRandall Stewart INP_WLOCK_RECHECK(inp); 170655bceb1eSRandall Stewart blk = find_and_ref_tcp_functions(&fsn); 170755bceb1eSRandall Stewart if (blk == NULL) { 170855bceb1eSRandall Stewart INP_WUNLOCK(inp); 170955bceb1eSRandall Stewart return (ENOENT); 171055bceb1eSRandall Stewart } 1711587d67c0SRandall Stewart if (tp->t_fb == blk) { 1712587d67c0SRandall Stewart /* You already have this */ 1713587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1714587d67c0SRandall Stewart INP_WUNLOCK(inp); 1715587d67c0SRandall Stewart return (0); 1716587d67c0SRandall Stewart } 1717587d67c0SRandall Stewart if (tp->t_state != TCPS_CLOSED) { 1718587d67c0SRandall Stewart /* 1719587d67c0SRandall Stewart * The user has advanced the state 1720587d67c0SRandall Stewart * past the initial point, we may not 1721587d67c0SRandall Stewart * be able to switch. 1722587d67c0SRandall Stewart */ 1723587d67c0SRandall Stewart if (blk->tfb_tcp_handoff_ok != NULL) { 1724587d67c0SRandall Stewart /* 1725587d67c0SRandall Stewart * Does the stack provide a 1726587d67c0SRandall Stewart * query mechanism, if so it may 1727587d67c0SRandall Stewart * still be possible? 1728587d67c0SRandall Stewart */ 1729587d67c0SRandall Stewart error = (*blk->tfb_tcp_handoff_ok)(tp); 1730c6c0be27SMichael Tuexen } else 1731c6c0be27SMichael Tuexen error = EINVAL; 1732587d67c0SRandall Stewart if (error) { 1733587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1734587d67c0SRandall Stewart INP_WUNLOCK(inp); 1735587d67c0SRandall Stewart return(error); 1736587d67c0SRandall Stewart } 1737587d67c0SRandall Stewart } 173855bceb1eSRandall Stewart if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) { 173955bceb1eSRandall Stewart refcount_release(&blk->tfb_refcnt); 174055bceb1eSRandall Stewart INP_WUNLOCK(inp); 174155bceb1eSRandall Stewart return (ENOENT); 174255bceb1eSRandall Stewart } 174355bceb1eSRandall Stewart /* 174455bceb1eSRandall Stewart * Release the old refcnt, the 1745587d67c0SRandall Stewart * lookup acquired a ref on the 1746587d67c0SRandall Stewart * new one already. 174755bceb1eSRandall Stewart */ 1748587d67c0SRandall Stewart if (tp->t_fb->tfb_tcp_fb_fini) { 1749587d67c0SRandall Stewart /* 1750587d67c0SRandall Stewart * Tell the stack to cleanup with 0 i.e. 1751587d67c0SRandall Stewart * the tcb is not going away. 1752587d67c0SRandall Stewart */ 1753587d67c0SRandall Stewart (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0); 1754587d67c0SRandall Stewart } 17553ee9c3c4SRandall Stewart #ifdef TCPHPTS 17563ee9c3c4SRandall Stewart /* Assure that we are not on any hpts */ 17573ee9c3c4SRandall Stewart tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL); 17583ee9c3c4SRandall Stewart #endif 17593ee9c3c4SRandall Stewart if (blk->tfb_tcp_fb_init) { 17603ee9c3c4SRandall Stewart error = (*blk->tfb_tcp_fb_init)(tp); 17613ee9c3c4SRandall Stewart if (error) { 17623ee9c3c4SRandall Stewart refcount_release(&blk->tfb_refcnt); 17633ee9c3c4SRandall Stewart if (tp->t_fb->tfb_tcp_fb_init) { 17643ee9c3c4SRandall Stewart if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) { 17653ee9c3c4SRandall Stewart /* Fall back failed, drop the connection */ 17663ee9c3c4SRandall Stewart INP_WUNLOCK(inp); 17673ee9c3c4SRandall Stewart soabort(so); 17683ee9c3c4SRandall Stewart return(error); 17693ee9c3c4SRandall Stewart } 17703ee9c3c4SRandall Stewart } 17713ee9c3c4SRandall Stewart goto err_out; 17723ee9c3c4SRandall Stewart } 17733ee9c3c4SRandall Stewart } 177455bceb1eSRandall Stewart refcount_release(&tp->t_fb->tfb_refcnt); 177555bceb1eSRandall Stewart tp->t_fb = blk; 177655bceb1eSRandall Stewart #ifdef TCP_OFFLOAD 177755bceb1eSRandall Stewart if (tp->t_flags & TF_TOE) { 177855bceb1eSRandall Stewart tcp_offload_ctloutput(tp, sopt->sopt_dir, 177955bceb1eSRandall Stewart sopt->sopt_name); 178055bceb1eSRandall Stewart } 178155bceb1eSRandall Stewart #endif 17823ee9c3c4SRandall Stewart err_out: 178355bceb1eSRandall Stewart INP_WUNLOCK(inp); 178455bceb1eSRandall Stewart return (error); 178555bceb1eSRandall Stewart } else if ((sopt->sopt_dir == SOPT_GET) && 178655bceb1eSRandall Stewart (sopt->sopt_name == TCP_FUNCTION_BLK)) { 1787c73b6f4dSEd Maste strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name, 1788c73b6f4dSEd Maste TCP_FUNCTION_NAME_LEN_MAX); 1789c73b6f4dSEd Maste fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0'; 179055bceb1eSRandall Stewart fsn.pcbcnt = tp->t_fb->tfb_refcnt; 179155bceb1eSRandall Stewart INP_WUNLOCK(inp); 179255bceb1eSRandall Stewart error = sooptcopyout(sopt, &fsn, sizeof fsn); 179355bceb1eSRandall Stewart return (error); 179455bceb1eSRandall Stewart } 179555bceb1eSRandall Stewart /* Pass in the INP locked, called must unlock it */ 179655bceb1eSRandall Stewart return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp)); 179755bceb1eSRandall Stewart } 179855bceb1eSRandall Stewart 17992529f56eSJonathan T. Looney /* 18002529f56eSJonathan T. Looney * If this assert becomes untrue, we need to change the size of the buf 18012529f56eSJonathan T. Looney * variable in tcp_default_ctloutput(). 18022529f56eSJonathan T. Looney */ 18032529f56eSJonathan T. Looney #ifdef CTASSERT 18042529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN); 18052529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN); 18062529f56eSJonathan T. Looney #endif 18072529f56eSJonathan T. Looney 180855bceb1eSRandall Stewart int 180955bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp) 181055bceb1eSRandall Stewart { 181155bceb1eSRandall Stewart int error, opt, optval; 181255bceb1eSRandall Stewart u_int ui; 181355bceb1eSRandall Stewart struct tcp_info ti; 1814b2e60773SJohn Baldwin #ifdef KERN_TLS 1815b2e60773SJohn Baldwin struct tls_enable tls; 1816b2e60773SJohn Baldwin #endif 181755bceb1eSRandall Stewart struct cc_algo *algo; 18182529f56eSJonathan T. Looney char *pbuf, buf[TCP_LOG_ID_LEN]; 1819af6fef3aSGleb Smirnoff size_t len; 1820df8bae1dSRodney W. Grimes 1821d519cedbSGleb Smirnoff /* 1822d519cedbSGleb Smirnoff * For TCP_CCALGOOPT forward the control to CC module, for both 1823d519cedbSGleb Smirnoff * SOPT_SET and SOPT_GET. 1824d519cedbSGleb Smirnoff */ 1825d519cedbSGleb Smirnoff switch (sopt->sopt_name) { 1826d519cedbSGleb Smirnoff case TCP_CCALGOOPT: 1827d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 1828c8b53cedSMichael Tuexen if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT) 1829c8b53cedSMichael Tuexen return (EINVAL); 1830af6fef3aSGleb Smirnoff pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO); 1831af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize, 1832d519cedbSGleb Smirnoff sopt->sopt_valsize); 1833d519cedbSGleb Smirnoff if (error) { 1834af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 1835d519cedbSGleb Smirnoff return (error); 1836d519cedbSGleb Smirnoff } 1837bac5bedfSConrad Meyer INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP)); 1838d519cedbSGleb Smirnoff if (CC_ALGO(tp)->ctl_output != NULL) 1839af6fef3aSGleb Smirnoff error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf); 1840d519cedbSGleb Smirnoff else 1841d519cedbSGleb Smirnoff error = ENOENT; 1842d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 1843d519cedbSGleb Smirnoff if (error == 0 && sopt->sopt_dir == SOPT_GET) 1844af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize); 1845af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 1846d519cedbSGleb Smirnoff return (error); 1847d519cedbSGleb Smirnoff } 1848d519cedbSGleb Smirnoff 1849cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1850cfe8b629SGarrett Wollman case SOPT_SET: 1851cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1852fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 185388f6b043SBruce M Simpson case TCP_MD5SIG: 1854fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED()) { 18558501a69cSRobert Watson INP_WUNLOCK(inp); 1856fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 1857fcf59617SAndrey V. Elsukov } 1858fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 18591cfd4b53SBruce M Simpson if (error) 18601e8f5ffaSRobert Watson return (error); 186109fe6320SNavdeep Parhar goto unlock_and_done; 1862fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 186309fe6320SNavdeep Parhar 1864df8bae1dSRodney W. Grimes case TCP_NODELAY: 1865cfe8b629SGarrett Wollman case TCP_NOOPT: 18668501a69cSRobert Watson INP_WUNLOCK(inp); 1867cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1868cfe8b629SGarrett Wollman sizeof optval); 1869cfe8b629SGarrett Wollman if (error) 18701e8f5ffaSRobert Watson return (error); 1871cfe8b629SGarrett Wollman 18728501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1873cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1874cfe8b629SGarrett Wollman case TCP_NODELAY: 1875cfe8b629SGarrett Wollman opt = TF_NODELAY; 1876cfe8b629SGarrett Wollman break; 1877cfe8b629SGarrett Wollman case TCP_NOOPT: 1878cfe8b629SGarrett Wollman opt = TF_NOOPT; 1879cfe8b629SGarrett Wollman break; 1880cfe8b629SGarrett Wollman default: 1881cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 1882cfe8b629SGarrett Wollman break; 1883cfe8b629SGarrett Wollman } 1884cfe8b629SGarrett Wollman 1885cfe8b629SGarrett Wollman if (optval) 1886cfe8b629SGarrett Wollman tp->t_flags |= opt; 1887df8bae1dSRodney W. Grimes else 1888cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 188909fe6320SNavdeep Parhar unlock_and_done: 189009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 189109fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 189209fe6320SNavdeep Parhar tcp_offload_ctloutput(tp, sopt->sopt_dir, 189309fe6320SNavdeep Parhar sopt->sopt_name); 189409fe6320SNavdeep Parhar } 189509fe6320SNavdeep Parhar #endif 18968501a69cSRobert Watson INP_WUNLOCK(inp); 1897df8bae1dSRodney W. Grimes break; 1898df8bae1dSRodney W. Grimes 1899007581c0SJonathan Lemon case TCP_NOPUSH: 19008501a69cSRobert Watson INP_WUNLOCK(inp); 1901007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 1902007581c0SJonathan Lemon sizeof optval); 1903007581c0SJonathan Lemon if (error) 19041e8f5ffaSRobert Watson return (error); 1905007581c0SJonathan Lemon 19068501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1907007581c0SJonathan Lemon if (optval) 1908007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 1909d28b9e89SJohn Baldwin else if (tp->t_flags & TF_NOPUSH) { 1910007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 1911d28b9e89SJohn Baldwin if (TCPS_HAVEESTABLISHED(tp->t_state)) 191255bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 1913007581c0SJonathan Lemon } 191409fe6320SNavdeep Parhar goto unlock_and_done; 1915007581c0SJonathan Lemon 1916df8bae1dSRodney W. Grimes case TCP_MAXSEG: 19178501a69cSRobert Watson INP_WUNLOCK(inp); 1918cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1919cfe8b629SGarrett Wollman sizeof optval); 1920cfe8b629SGarrett Wollman if (error) 19211e8f5ffaSRobert Watson return (error); 1922df8bae1dSRodney W. Grimes 19238501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 192453369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 1925603724d3SBjoern A. Zeeb optval + 40 >= V_tcp_minmss) 1926cfe8b629SGarrett Wollman tp->t_maxseg = optval; 1927a0292f23SGarrett Wollman else 1928a0292f23SGarrett Wollman error = EINVAL; 192909fe6320SNavdeep Parhar goto unlock_and_done; 1930a0292f23SGarrett Wollman 1931b8af5dfaSRobert Watson case TCP_INFO: 19328501a69cSRobert Watson INP_WUNLOCK(inp); 1933b8af5dfaSRobert Watson error = EINVAL; 1934b8af5dfaSRobert Watson break; 1935b8af5dfaSRobert Watson 1936dbc42409SLawrence Stewart case TCP_CONGESTION: 1937dbc42409SLawrence Stewart INP_WUNLOCK(inp); 1938af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1); 1939af6fef3aSGleb Smirnoff if (error) 1940dbc42409SLawrence Stewart break; 1941af6fef3aSGleb Smirnoff buf[sopt->sopt_valsize] = '\0'; 1942af6fef3aSGleb Smirnoff INP_WLOCK_RECHECK(inp); 194373e263b1SGleb Smirnoff CC_LIST_RLOCK(); 194473e263b1SGleb Smirnoff STAILQ_FOREACH(algo, &cc_list, entries) 194573e263b1SGleb Smirnoff if (strncmp(buf, algo->name, 194673e263b1SGleb Smirnoff TCP_CA_NAME_MAX) == 0) 194773e263b1SGleb Smirnoff break; 194873e263b1SGleb Smirnoff CC_LIST_RUNLOCK(); 194973e263b1SGleb Smirnoff if (algo == NULL) { 1950af6fef3aSGleb Smirnoff INP_WUNLOCK(inp); 195173e263b1SGleb Smirnoff error = EINVAL; 195273e263b1SGleb Smirnoff break; 195373e263b1SGleb Smirnoff } 1954dbc42409SLawrence Stewart /* 195573e263b1SGleb Smirnoff * We hold a write lock over the tcb so it's safe to 195673e263b1SGleb Smirnoff * do these things without ordering concerns. 1957dbc42409SLawrence Stewart */ 1958dbc42409SLawrence Stewart if (CC_ALGO(tp)->cb_destroy != NULL) 1959dbc42409SLawrence Stewart CC_ALGO(tp)->cb_destroy(tp->ccv); 196022699887SMatt Macy CC_DATA(tp) = NULL; 1961dbc42409SLawrence Stewart CC_ALGO(tp) = algo; 1962dbc42409SLawrence Stewart /* 196373e263b1SGleb Smirnoff * If something goes pear shaped initialising the new 196473e263b1SGleb Smirnoff * algo, fall back to newreno (which does not 196573e263b1SGleb Smirnoff * require initialisation). 1966dbc42409SLawrence Stewart */ 196773e263b1SGleb Smirnoff if (algo->cb_init != NULL && 196873e263b1SGleb Smirnoff algo->cb_init(tp->ccv) != 0) { 1969dbc42409SLawrence Stewart CC_ALGO(tp) = &newreno_cc_algo; 1970dbc42409SLawrence Stewart /* 197173e263b1SGleb Smirnoff * The only reason init should fail is 1972dbc42409SLawrence Stewart * because of malloc. 1973dbc42409SLawrence Stewart */ 1974dbc42409SLawrence Stewart error = ENOMEM; 1975dbc42409SLawrence Stewart } 197673e263b1SGleb Smirnoff INP_WUNLOCK(inp); 197773e263b1SGleb Smirnoff break; 1978dbc42409SLawrence Stewart 1979b2e60773SJohn Baldwin #ifdef KERN_TLS 1980b2e60773SJohn Baldwin case TCP_TXTLS_ENABLE: 1981b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1982b2e60773SJohn Baldwin error = sooptcopyin(sopt, &tls, sizeof(tls), 1983b2e60773SJohn Baldwin sizeof(tls)); 1984b2e60773SJohn Baldwin if (error) 1985b2e60773SJohn Baldwin break; 1986b2e60773SJohn Baldwin error = ktls_enable_tx(so, &tls); 1987b2e60773SJohn Baldwin break; 1988b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 1989b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1990b2e60773SJohn Baldwin error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 1991b2e60773SJohn Baldwin if (error) 1992b2e60773SJohn Baldwin return (error); 1993b2e60773SJohn Baldwin 1994b2e60773SJohn Baldwin INP_WLOCK_RECHECK(inp); 1995b2e60773SJohn Baldwin error = ktls_set_tx_mode(so, ui); 1996b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1997b2e60773SJohn Baldwin break; 1998b2e60773SJohn Baldwin #endif 1999b2e60773SJohn Baldwin 20009077f387SGleb Smirnoff case TCP_KEEPIDLE: 20019077f387SGleb Smirnoff case TCP_KEEPINTVL: 20029077f387SGleb Smirnoff case TCP_KEEPINIT: 20039077f387SGleb Smirnoff INP_WUNLOCK(inp); 20049077f387SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 20059077f387SGleb Smirnoff if (error) 20069077f387SGleb Smirnoff return (error); 20079077f387SGleb Smirnoff 20089077f387SGleb Smirnoff if (ui > (UINT_MAX / hz)) { 20099077f387SGleb Smirnoff error = EINVAL; 20109077f387SGleb Smirnoff break; 20119077f387SGleb Smirnoff } 20129077f387SGleb Smirnoff ui *= hz; 20139077f387SGleb Smirnoff 20149077f387SGleb Smirnoff INP_WLOCK_RECHECK(inp); 20159077f387SGleb Smirnoff switch (sopt->sopt_name) { 20169077f387SGleb Smirnoff case TCP_KEEPIDLE: 20179077f387SGleb Smirnoff tp->t_keepidle = ui; 20189077f387SGleb Smirnoff /* 20199077f387SGleb Smirnoff * XXX: better check current remaining 20209077f387SGleb Smirnoff * timeout and "merge" it with new value. 20219077f387SGleb Smirnoff */ 20229077f387SGleb Smirnoff if ((tp->t_state > TCPS_LISTEN) && 20239077f387SGleb Smirnoff (tp->t_state <= TCPS_CLOSING)) 20249077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 20259077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 20269077f387SGleb Smirnoff break; 20279077f387SGleb Smirnoff case TCP_KEEPINTVL: 20289077f387SGleb Smirnoff tp->t_keepintvl = ui; 20299077f387SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 20309077f387SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 20319077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 20329077f387SGleb Smirnoff TP_MAXIDLE(tp)); 20339077f387SGleb Smirnoff break; 20349077f387SGleb Smirnoff case TCP_KEEPINIT: 20359077f387SGleb Smirnoff tp->t_keepinit = ui; 20369077f387SGleb Smirnoff if (tp->t_state == TCPS_SYN_RECEIVED || 20379077f387SGleb Smirnoff tp->t_state == TCPS_SYN_SENT) 20389077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 20399077f387SGleb Smirnoff TP_KEEPINIT(tp)); 20409077f387SGleb Smirnoff break; 20419077f387SGleb Smirnoff } 204209fe6320SNavdeep Parhar goto unlock_and_done; 20439077f387SGleb Smirnoff 204485c05144SGleb Smirnoff case TCP_KEEPCNT: 204585c05144SGleb Smirnoff INP_WUNLOCK(inp); 204685c05144SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 204785c05144SGleb Smirnoff if (error) 204885c05144SGleb Smirnoff return (error); 204985c05144SGleb Smirnoff 205085c05144SGleb Smirnoff INP_WLOCK_RECHECK(inp); 205185c05144SGleb Smirnoff tp->t_keepcnt = ui; 205285c05144SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 205385c05144SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 205485c05144SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 205585c05144SGleb Smirnoff TP_MAXIDLE(tp)); 205685c05144SGleb Smirnoff goto unlock_and_done; 205785c05144SGleb Smirnoff 205886a996e6SHiren Panchasara #ifdef TCPPCAP 205986a996e6SHiren Panchasara case TCP_PCAP_OUT: 206086a996e6SHiren Panchasara case TCP_PCAP_IN: 206186a996e6SHiren Panchasara INP_WUNLOCK(inp); 206286a996e6SHiren Panchasara error = sooptcopyin(sopt, &optval, sizeof optval, 206386a996e6SHiren Panchasara sizeof optval); 206486a996e6SHiren Panchasara if (error) 206586a996e6SHiren Panchasara return (error); 206686a996e6SHiren Panchasara 206786a996e6SHiren Panchasara INP_WLOCK_RECHECK(inp); 206886a996e6SHiren Panchasara if (optval >= 0) 206986a996e6SHiren Panchasara tcp_pcap_set_sock_max(TCP_PCAP_OUT ? 207086a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts), 207186a996e6SHiren Panchasara optval); 207286a996e6SHiren Panchasara else 207386a996e6SHiren Panchasara error = EINVAL; 207486a996e6SHiren Panchasara goto unlock_and_done; 207586a996e6SHiren Panchasara #endif 207686a996e6SHiren Panchasara 2077c560df6fSPatrick Kelsey case TCP_FASTOPEN: { 2078c560df6fSPatrick Kelsey struct tcp_fastopen tfo_optval; 2079c560df6fSPatrick Kelsey 2080281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2081c560df6fSPatrick Kelsey if (!V_tcp_fastopen_client_enable && 2082c560df6fSPatrick Kelsey !V_tcp_fastopen_server_enable) 2083281a0fd4SPatrick Kelsey return (EPERM); 2084281a0fd4SPatrick Kelsey 2085c560df6fSPatrick Kelsey error = sooptcopyin(sopt, &tfo_optval, 2086c560df6fSPatrick Kelsey sizeof(tfo_optval), sizeof(int)); 2087281a0fd4SPatrick Kelsey if (error) 2088281a0fd4SPatrick Kelsey return (error); 2089281a0fd4SPatrick Kelsey 2090281a0fd4SPatrick Kelsey INP_WLOCK_RECHECK(inp); 2091c560df6fSPatrick Kelsey if (tfo_optval.enable) { 2092c560df6fSPatrick Kelsey if (tp->t_state == TCPS_LISTEN) { 2093c560df6fSPatrick Kelsey if (!V_tcp_fastopen_server_enable) { 2094c560df6fSPatrick Kelsey error = EPERM; 2095c560df6fSPatrick Kelsey goto unlock_and_done; 2096c560df6fSPatrick Kelsey } 2097c560df6fSPatrick Kelsey 2098281a0fd4SPatrick Kelsey tp->t_flags |= TF_FASTOPEN; 2099c560df6fSPatrick Kelsey if (tp->t_tfo_pending == NULL) 2100281a0fd4SPatrick Kelsey tp->t_tfo_pending = 2101281a0fd4SPatrick Kelsey tcp_fastopen_alloc_counter(); 2102c560df6fSPatrick Kelsey } else { 2103c560df6fSPatrick Kelsey /* 2104c560df6fSPatrick Kelsey * If a pre-shared key was provided, 2105c560df6fSPatrick Kelsey * stash it in the client cookie 2106c560df6fSPatrick Kelsey * field of the tcpcb for use during 2107c560df6fSPatrick Kelsey * connect. 2108c560df6fSPatrick Kelsey */ 2109c560df6fSPatrick Kelsey if (sopt->sopt_valsize == 2110c560df6fSPatrick Kelsey sizeof(tfo_optval)) { 2111c560df6fSPatrick Kelsey memcpy(tp->t_tfo_cookie.client, 2112c560df6fSPatrick Kelsey tfo_optval.psk, 2113c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN); 2114c560df6fSPatrick Kelsey tp->t_tfo_client_cookie_len = 2115c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN; 2116c560df6fSPatrick Kelsey } 2117c560df6fSPatrick Kelsey tp->t_flags |= TF_FASTOPEN; 2118c560df6fSPatrick Kelsey } 2119281a0fd4SPatrick Kelsey } else 2120281a0fd4SPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 2121281a0fd4SPatrick Kelsey goto unlock_and_done; 2122c560df6fSPatrick Kelsey } 2123281a0fd4SPatrick Kelsey 2124e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 21252529f56eSJonathan T. Looney case TCP_LOG: 21262529f56eSJonathan T. Looney INP_WUNLOCK(inp); 21272529f56eSJonathan T. Looney error = sooptcopyin(sopt, &optval, sizeof optval, 21282529f56eSJonathan T. Looney sizeof optval); 21292529f56eSJonathan T. Looney if (error) 21302529f56eSJonathan T. Looney return (error); 21312529f56eSJonathan T. Looney 21322529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 21332529f56eSJonathan T. Looney error = tcp_log_state_change(tp, optval); 21342529f56eSJonathan T. Looney goto unlock_and_done; 21352529f56eSJonathan T. Looney 21362529f56eSJonathan T. Looney case TCP_LOGBUF: 21372529f56eSJonathan T. Looney INP_WUNLOCK(inp); 21382529f56eSJonathan T. Looney error = EINVAL; 21392529f56eSJonathan T. Looney break; 21402529f56eSJonathan T. Looney 21412529f56eSJonathan T. Looney case TCP_LOGID: 21422529f56eSJonathan T. Looney INP_WUNLOCK(inp); 21432529f56eSJonathan T. Looney error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0); 21442529f56eSJonathan T. Looney if (error) 21452529f56eSJonathan T. Looney break; 21462529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 21472529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 21482529f56eSJonathan T. Looney error = tcp_log_set_id(tp, buf); 21492529f56eSJonathan T. Looney /* tcp_log_set_id() unlocks the INP. */ 21502529f56eSJonathan T. Looney break; 21512529f56eSJonathan T. Looney 21522529f56eSJonathan T. Looney case TCP_LOGDUMP: 21532529f56eSJonathan T. Looney case TCP_LOGDUMPID: 21542529f56eSJonathan T. Looney INP_WUNLOCK(inp); 21552529f56eSJonathan T. Looney error = 21562529f56eSJonathan T. Looney sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0); 21572529f56eSJonathan T. Looney if (error) 21582529f56eSJonathan T. Looney break; 21592529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 21602529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 21612529f56eSJonathan T. Looney if (sopt->sopt_name == TCP_LOGDUMP) { 21622529f56eSJonathan T. Looney error = tcp_log_dump_tp_logbuf(tp, buf, 21632529f56eSJonathan T. Looney M_WAITOK, true); 21642529f56eSJonathan T. Looney INP_WUNLOCK(inp); 21652529f56eSJonathan T. Looney } else { 21662529f56eSJonathan T. Looney tcp_log_dump_tp_bucket_logbufs(tp, buf); 21672529f56eSJonathan T. Looney /* 21682529f56eSJonathan T. Looney * tcp_log_dump_tp_bucket_logbufs() drops the 21692529f56eSJonathan T. Looney * INP lock. 21702529f56eSJonathan T. Looney */ 21712529f56eSJonathan T. Looney } 21722529f56eSJonathan T. Looney break; 2173e24e5683SJonathan T. Looney #endif 21742529f56eSJonathan T. Looney 2175df8bae1dSRodney W. Grimes default: 21768501a69cSRobert Watson INP_WUNLOCK(inp); 2177df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2178df8bae1dSRodney W. Grimes break; 2179df8bae1dSRodney W. Grimes } 2180df8bae1dSRodney W. Grimes break; 2181df8bae1dSRodney W. Grimes 2182cfe8b629SGarrett Wollman case SOPT_GET: 21831e8f5ffaSRobert Watson tp = intotcpcb(inp); 2184cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2185fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 218688f6b043SBruce M Simpson case TCP_MD5SIG: 2187fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED()) { 21888501a69cSRobert Watson INP_WUNLOCK(inp); 2189fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 2190fcf59617SAndrey V. Elsukov } 2191fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 21921cfd4b53SBruce M Simpson break; 2193265ed012SBruce M Simpson #endif 21941e8f5ffaSRobert Watson 2195df8bae1dSRodney W. Grimes case TCP_NODELAY: 2196cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 21978501a69cSRobert Watson INP_WUNLOCK(inp); 2198b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2199df8bae1dSRodney W. Grimes break; 2200df8bae1dSRodney W. Grimes case TCP_MAXSEG: 2201cfe8b629SGarrett Wollman optval = tp->t_maxseg; 22028501a69cSRobert Watson INP_WUNLOCK(inp); 2203b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2204df8bae1dSRodney W. Grimes break; 2205a0292f23SGarrett Wollman case TCP_NOOPT: 2206cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 22078501a69cSRobert Watson INP_WUNLOCK(inp); 2208b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2209a0292f23SGarrett Wollman break; 2210a0292f23SGarrett Wollman case TCP_NOPUSH: 2211cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 22128501a69cSRobert Watson INP_WUNLOCK(inp); 2213b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2214b8af5dfaSRobert Watson break; 2215b8af5dfaSRobert Watson case TCP_INFO: 2216b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 22178501a69cSRobert Watson INP_WUNLOCK(inp); 2218b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 2219a0292f23SGarrett Wollman break; 2220dbc42409SLawrence Stewart case TCP_CONGESTION: 2221af6fef3aSGleb Smirnoff len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 2222dbc42409SLawrence Stewart INP_WUNLOCK(inp); 2223af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, buf, len + 1); 2224dbc42409SLawrence Stewart break; 22252f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 22262f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 22272f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 22282f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 22292f3eb7f4SGleb Smirnoff switch (sopt->sopt_name) { 22302f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 22315a17b6adSMichael Tuexen ui = TP_KEEPIDLE(tp) / hz; 22322f3eb7f4SGleb Smirnoff break; 22332f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 22345a17b6adSMichael Tuexen ui = TP_KEEPINTVL(tp) / hz; 22352f3eb7f4SGleb Smirnoff break; 22362f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 22375a17b6adSMichael Tuexen ui = TP_KEEPINIT(tp) / hz; 22382f3eb7f4SGleb Smirnoff break; 22392f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 22405a17b6adSMichael Tuexen ui = TP_KEEPCNT(tp); 22412f3eb7f4SGleb Smirnoff break; 22422f3eb7f4SGleb Smirnoff } 22432f3eb7f4SGleb Smirnoff INP_WUNLOCK(inp); 22442f3eb7f4SGleb Smirnoff error = sooptcopyout(sopt, &ui, sizeof(ui)); 22452f3eb7f4SGleb Smirnoff break; 224686a996e6SHiren Panchasara #ifdef TCPPCAP 224786a996e6SHiren Panchasara case TCP_PCAP_OUT: 224886a996e6SHiren Panchasara case TCP_PCAP_IN: 224986a996e6SHiren Panchasara optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ? 225086a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts)); 225186a996e6SHiren Panchasara INP_WUNLOCK(inp); 225286a996e6SHiren Panchasara error = sooptcopyout(sopt, &optval, sizeof optval); 225386a996e6SHiren Panchasara break; 225486a996e6SHiren Panchasara #endif 2255281a0fd4SPatrick Kelsey case TCP_FASTOPEN: 2256281a0fd4SPatrick Kelsey optval = tp->t_flags & TF_FASTOPEN; 2257281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2258281a0fd4SPatrick Kelsey error = sooptcopyout(sopt, &optval, sizeof optval); 2259281a0fd4SPatrick Kelsey break; 2260e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 22612529f56eSJonathan T. Looney case TCP_LOG: 22622529f56eSJonathan T. Looney optval = tp->t_logstate; 22632529f56eSJonathan T. Looney INP_WUNLOCK(inp); 22642529f56eSJonathan T. Looney error = sooptcopyout(sopt, &optval, sizeof(optval)); 22652529f56eSJonathan T. Looney break; 22662529f56eSJonathan T. Looney case TCP_LOGBUF: 22672529f56eSJonathan T. Looney /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */ 22682529f56eSJonathan T. Looney error = tcp_log_getlogbuf(sopt, tp); 22692529f56eSJonathan T. Looney break; 22702529f56eSJonathan T. Looney case TCP_LOGID: 22712529f56eSJonathan T. Looney len = tcp_log_get_id(tp, buf); 22722529f56eSJonathan T. Looney INP_WUNLOCK(inp); 22732529f56eSJonathan T. Looney error = sooptcopyout(sopt, buf, len + 1); 22742529f56eSJonathan T. Looney break; 22752529f56eSJonathan T. Looney case TCP_LOGDUMP: 22762529f56eSJonathan T. Looney case TCP_LOGDUMPID: 22772529f56eSJonathan T. Looney INP_WUNLOCK(inp); 22782529f56eSJonathan T. Looney error = EINVAL; 22792529f56eSJonathan T. Looney break; 2280e24e5683SJonathan T. Looney #endif 2281b2e60773SJohn Baldwin #ifdef KERN_TLS 2282b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2283b2e60773SJohn Baldwin optval = ktls_get_tx_mode(so); 2284b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2285b2e60773SJohn Baldwin error = sooptcopyout(sopt, &optval, sizeof(optval)); 2286b2e60773SJohn Baldwin break; 2287b2e60773SJohn Baldwin #endif 2288df8bae1dSRodney W. Grimes default: 22898501a69cSRobert Watson INP_WUNLOCK(inp); 2290df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2291df8bae1dSRodney W. Grimes break; 2292df8bae1dSRodney W. Grimes } 2293df8bae1dSRodney W. Grimes break; 2294df8bae1dSRodney W. Grimes } 2295df8bae1dSRodney W. Grimes return (error); 2296df8bae1dSRodney W. Grimes } 22978501a69cSRobert Watson #undef INP_WLOCK_RECHECK 2298bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP 2299df8bae1dSRodney W. Grimes 230026e30fbbSDavid Greenman /* 2301df8bae1dSRodney W. Grimes * Attach TCP protocol to socket, allocating 2302df8bae1dSRodney W. Grimes * internet protocol control block, tcp control block, 2303df8bae1dSRodney W. Grimes * bufer space, and entering LISTEN state if to accept connections. 2304df8bae1dSRodney W. Grimes */ 23050312fbe9SPoul-Henning Kamp static int 2306ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so) 2307df8bae1dSRodney W. Grimes { 2308ad3f9ab3SAndre Oppermann struct tcpcb *tp; 2309df8bae1dSRodney W. Grimes struct inpcb *inp; 23106573d758SMatt Macy struct epoch_tracker et; 2311df8bae1dSRodney W. Grimes int error; 2312df8bae1dSRodney W. Grimes 2313df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 2314e233e2acSAndre Oppermann error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace); 2315df8bae1dSRodney W. Grimes if (error) 2316df8bae1dSRodney W. Grimes return (error); 2317df8bae1dSRodney W. Grimes } 23186741ecf5SAndre Oppermann so->so_rcv.sb_flags |= SB_AUTOSIZE; 23196741ecf5SAndre Oppermann so->so_snd.sb_flags |= SB_AUTOSIZE; 23206573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, et); 2321603724d3SBjoern A. Zeeb error = in_pcballoc(so, &V_tcbinfo); 2322f2de87feSRobert Watson if (error) { 23236573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 2324df8bae1dSRodney W. Grimes return (error); 2325f2de87feSRobert Watson } 2326df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 2327fb59c426SYoshinobu Inoue #ifdef INET6 23285cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 2329fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 233063ec505aSMichael Tuexen if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 233163ec505aSMichael Tuexen inp->inp_vflag |= INP_IPV4; 2332fb59c426SYoshinobu Inoue inp->in6p_hops = -1; /* use kernel default */ 2333fb59c426SYoshinobu Inoue } 2334fb59c426SYoshinobu Inoue else 2335fb59c426SYoshinobu Inoue #endif 2336cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 2337df8bae1dSRodney W. Grimes tp = tcp_newtcpcb(inp); 2338623dce13SRobert Watson if (tp == NULL) { 2339df8bae1dSRodney W. Grimes in_pcbdetach(inp); 23400206cdb8SBjoern A. Zeeb in_pcbfree(inp); 23416573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 2342df8bae1dSRodney W. Grimes return (ENOBUFS); 2343df8bae1dSRodney W. Grimes } 2344df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 23458501a69cSRobert Watson INP_WUNLOCK(inp); 23466573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 2347bf840a17SGleb Smirnoff TCPSTATES_INC(TCPS_CLOSED); 2348df8bae1dSRodney W. Grimes return (0); 2349df8bae1dSRodney W. Grimes } 2350df8bae1dSRodney W. Grimes 2351df8bae1dSRodney W. Grimes /* 2352df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 2353df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 2354df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 2355df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 2356df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 2357df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 2358df8bae1dSRodney W. Grimes */ 2359623dce13SRobert Watson static void 2360ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp) 2361df8bae1dSRodney W. Grimes { 2362e6e0b5ffSRobert Watson struct inpcb *inp = tp->t_inpcb; 2363e6e0b5ffSRobert Watson struct socket *so = inp->inp_socket; 2364e6e0b5ffSRobert Watson 2365ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 23668501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2367df8bae1dSRodney W. Grimes 2368623dce13SRobert Watson /* 2369623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 2370623dce13SRobert Watson * socket is still open. 2371623dce13SRobert Watson */ 23728db239dcSMichael Tuexen if (tp->t_state < TCPS_ESTABLISHED && 23738db239dcSMichael Tuexen !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) { 2374df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2375623dce13SRobert Watson KASSERT(tp != NULL, 2376623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 2377623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 2378243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 2379623dce13SRobert Watson KASSERT(tp != NULL, 2380623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 2381623dce13SRobert Watson } else { 2382df8bae1dSRodney W. Grimes soisdisconnecting(so); 2383df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 2384623dce13SRobert Watson tcp_usrclosed(tp); 2385ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 238655bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 2387df8bae1dSRodney W. Grimes } 2388df8bae1dSRodney W. Grimes } 2389df8bae1dSRodney W. Grimes 2390df8bae1dSRodney W. Grimes /* 2391df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 2392df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 2393df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 2394df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 2395df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 2396df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 2397df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 2398df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 2399df8bae1dSRodney W. Grimes */ 2400623dce13SRobert Watson static void 2401ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp) 2402df8bae1dSRodney W. Grimes { 2403df8bae1dSRodney W. Grimes 2404ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 24058501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2406e6e0b5ffSRobert Watson 2407df8bae1dSRodney W. Grimes switch (tp->t_state) { 2408df8bae1dSRodney W. Grimes case TCPS_LISTEN: 240909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 241009fe6320SNavdeep Parhar tcp_offload_listen_stop(tp); 241109fe6320SNavdeep Parhar #endif 2412550e9d42SHiren Panchasara tcp_state_change(tp, TCPS_CLOSED); 2413bc65987aSKip Macy /* FALLTHROUGH */ 2414bc65987aSKip Macy case TCPS_CLOSED: 2415df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2416623dce13SRobert Watson /* 2417623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 2418623dce13SRobert Watson * still open. 2419623dce13SRobert Watson */ 2420623dce13SRobert Watson KASSERT(tp != NULL, 2421623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 2422df8bae1dSRodney W. Grimes break; 2423df8bae1dSRodney W. Grimes 2424a0292f23SGarrett Wollman case TCPS_SYN_SENT: 2425df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2426a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 2427a0292f23SGarrett Wollman break; 2428a0292f23SGarrett Wollman 2429df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 243057f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2431df8bae1dSRodney W. Grimes break; 2432df8bae1dSRodney W. Grimes 2433df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 243457f60867SMark Johnston tcp_state_change(tp, TCPS_LAST_ACK); 2435df8bae1dSRodney W. Grimes break; 2436df8bae1dSRodney W. Grimes } 2437abc7d910SRobert Watson if (tp->t_state >= TCPS_FIN_WAIT_2) { 2438df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 2439abc7d910SRobert Watson /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 24407c72af87SMohan Srinivasan if (tp->t_state == TCPS_FIN_WAIT_2) { 24417c72af87SMohan Srinivasan int timeout; 24427c72af87SMohan Srinivasan 24437c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 24449077f387SGleb Smirnoff tcp_finwait2_timeout : TP_MAXIDLE(tp); 2445b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 2446b6239c4aSAndras Olah } 2447df8bae1dSRodney W. Grimes } 24487c72af87SMohan Srinivasan } 2449497057eeSRobert Watson 2450497057eeSRobert Watson #ifdef DDB 2451497057eeSRobert Watson static void 2452497057eeSRobert Watson db_print_indent(int indent) 2453497057eeSRobert Watson { 2454497057eeSRobert Watson int i; 2455497057eeSRobert Watson 2456497057eeSRobert Watson for (i = 0; i < indent; i++) 2457497057eeSRobert Watson db_printf(" "); 2458497057eeSRobert Watson } 2459497057eeSRobert Watson 2460497057eeSRobert Watson static void 2461497057eeSRobert Watson db_print_tstate(int t_state) 2462497057eeSRobert Watson { 2463497057eeSRobert Watson 2464497057eeSRobert Watson switch (t_state) { 2465497057eeSRobert Watson case TCPS_CLOSED: 2466497057eeSRobert Watson db_printf("TCPS_CLOSED"); 2467497057eeSRobert Watson return; 2468497057eeSRobert Watson 2469497057eeSRobert Watson case TCPS_LISTEN: 2470497057eeSRobert Watson db_printf("TCPS_LISTEN"); 2471497057eeSRobert Watson return; 2472497057eeSRobert Watson 2473497057eeSRobert Watson case TCPS_SYN_SENT: 2474497057eeSRobert Watson db_printf("TCPS_SYN_SENT"); 2475497057eeSRobert Watson return; 2476497057eeSRobert Watson 2477497057eeSRobert Watson case TCPS_SYN_RECEIVED: 2478497057eeSRobert Watson db_printf("TCPS_SYN_RECEIVED"); 2479497057eeSRobert Watson return; 2480497057eeSRobert Watson 2481497057eeSRobert Watson case TCPS_ESTABLISHED: 2482497057eeSRobert Watson db_printf("TCPS_ESTABLISHED"); 2483497057eeSRobert Watson return; 2484497057eeSRobert Watson 2485497057eeSRobert Watson case TCPS_CLOSE_WAIT: 2486497057eeSRobert Watson db_printf("TCPS_CLOSE_WAIT"); 2487497057eeSRobert Watson return; 2488497057eeSRobert Watson 2489497057eeSRobert Watson case TCPS_FIN_WAIT_1: 2490497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_1"); 2491497057eeSRobert Watson return; 2492497057eeSRobert Watson 2493497057eeSRobert Watson case TCPS_CLOSING: 2494497057eeSRobert Watson db_printf("TCPS_CLOSING"); 2495497057eeSRobert Watson return; 2496497057eeSRobert Watson 2497497057eeSRobert Watson case TCPS_LAST_ACK: 2498497057eeSRobert Watson db_printf("TCPS_LAST_ACK"); 2499497057eeSRobert Watson return; 2500497057eeSRobert Watson 2501497057eeSRobert Watson case TCPS_FIN_WAIT_2: 2502497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_2"); 2503497057eeSRobert Watson return; 2504497057eeSRobert Watson 2505497057eeSRobert Watson case TCPS_TIME_WAIT: 2506497057eeSRobert Watson db_printf("TCPS_TIME_WAIT"); 2507497057eeSRobert Watson return; 2508497057eeSRobert Watson 2509497057eeSRobert Watson default: 2510497057eeSRobert Watson db_printf("unknown"); 2511497057eeSRobert Watson return; 2512497057eeSRobert Watson } 2513497057eeSRobert Watson } 2514497057eeSRobert Watson 2515497057eeSRobert Watson static void 2516497057eeSRobert Watson db_print_tflags(u_int t_flags) 2517497057eeSRobert Watson { 2518497057eeSRobert Watson int comma; 2519497057eeSRobert Watson 2520497057eeSRobert Watson comma = 0; 2521497057eeSRobert Watson if (t_flags & TF_ACKNOW) { 2522497057eeSRobert Watson db_printf("%sTF_ACKNOW", comma ? ", " : ""); 2523497057eeSRobert Watson comma = 1; 2524497057eeSRobert Watson } 2525497057eeSRobert Watson if (t_flags & TF_DELACK) { 2526497057eeSRobert Watson db_printf("%sTF_DELACK", comma ? ", " : ""); 2527497057eeSRobert Watson comma = 1; 2528497057eeSRobert Watson } 2529497057eeSRobert Watson if (t_flags & TF_NODELAY) { 2530497057eeSRobert Watson db_printf("%sTF_NODELAY", comma ? ", " : ""); 2531497057eeSRobert Watson comma = 1; 2532497057eeSRobert Watson } 2533497057eeSRobert Watson if (t_flags & TF_NOOPT) { 2534497057eeSRobert Watson db_printf("%sTF_NOOPT", comma ? ", " : ""); 2535497057eeSRobert Watson comma = 1; 2536497057eeSRobert Watson } 2537497057eeSRobert Watson if (t_flags & TF_SENTFIN) { 2538497057eeSRobert Watson db_printf("%sTF_SENTFIN", comma ? ", " : ""); 2539497057eeSRobert Watson comma = 1; 2540497057eeSRobert Watson } 2541497057eeSRobert Watson if (t_flags & TF_REQ_SCALE) { 2542497057eeSRobert Watson db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 2543497057eeSRobert Watson comma = 1; 2544497057eeSRobert Watson } 2545497057eeSRobert Watson if (t_flags & TF_RCVD_SCALE) { 2546497057eeSRobert Watson db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 2547497057eeSRobert Watson comma = 1; 2548497057eeSRobert Watson } 2549497057eeSRobert Watson if (t_flags & TF_REQ_TSTMP) { 2550497057eeSRobert Watson db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 2551497057eeSRobert Watson comma = 1; 2552497057eeSRobert Watson } 2553497057eeSRobert Watson if (t_flags & TF_RCVD_TSTMP) { 2554497057eeSRobert Watson db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 2555497057eeSRobert Watson comma = 1; 2556497057eeSRobert Watson } 2557497057eeSRobert Watson if (t_flags & TF_SACK_PERMIT) { 2558497057eeSRobert Watson db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 2559497057eeSRobert Watson comma = 1; 2560497057eeSRobert Watson } 2561497057eeSRobert Watson if (t_flags & TF_NEEDSYN) { 2562497057eeSRobert Watson db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 2563497057eeSRobert Watson comma = 1; 2564497057eeSRobert Watson } 2565497057eeSRobert Watson if (t_flags & TF_NEEDFIN) { 2566497057eeSRobert Watson db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 2567497057eeSRobert Watson comma = 1; 2568497057eeSRobert Watson } 2569497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 2570497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 2571497057eeSRobert Watson comma = 1; 2572497057eeSRobert Watson } 2573497057eeSRobert Watson if (t_flags & TF_MORETOCOME) { 2574497057eeSRobert Watson db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 2575497057eeSRobert Watson comma = 1; 2576497057eeSRobert Watson } 2577497057eeSRobert Watson if (t_flags & TF_LQ_OVERFLOW) { 2578497057eeSRobert Watson db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 2579497057eeSRobert Watson comma = 1; 2580497057eeSRobert Watson } 2581497057eeSRobert Watson if (t_flags & TF_LASTIDLE) { 2582497057eeSRobert Watson db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 2583497057eeSRobert Watson comma = 1; 2584497057eeSRobert Watson } 2585497057eeSRobert Watson if (t_flags & TF_RXWIN0SENT) { 2586497057eeSRobert Watson db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 2587497057eeSRobert Watson comma = 1; 2588497057eeSRobert Watson } 2589497057eeSRobert Watson if (t_flags & TF_FASTRECOVERY) { 2590497057eeSRobert Watson db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 2591497057eeSRobert Watson comma = 1; 2592497057eeSRobert Watson } 2593dbc42409SLawrence Stewart if (t_flags & TF_CONGRECOVERY) { 2594dbc42409SLawrence Stewart db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 2595dbc42409SLawrence Stewart comma = 1; 2596dbc42409SLawrence Stewart } 2597497057eeSRobert Watson if (t_flags & TF_WASFRECOVERY) { 2598497057eeSRobert Watson db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 2599497057eeSRobert Watson comma = 1; 2600497057eeSRobert Watson } 2601497057eeSRobert Watson if (t_flags & TF_SIGNATURE) { 2602497057eeSRobert Watson db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 2603497057eeSRobert Watson comma = 1; 2604497057eeSRobert Watson } 2605497057eeSRobert Watson if (t_flags & TF_FORCEDATA) { 2606497057eeSRobert Watson db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 2607497057eeSRobert Watson comma = 1; 2608497057eeSRobert Watson } 2609497057eeSRobert Watson if (t_flags & TF_TSO) { 2610497057eeSRobert Watson db_printf("%sTF_TSO", comma ? ", " : ""); 2611497057eeSRobert Watson comma = 1; 2612497057eeSRobert Watson } 2613f2512ba1SRui Paulo if (t_flags & TF_ECN_PERMIT) { 2614f2512ba1SRui Paulo db_printf("%sTF_ECN_PERMIT", comma ? ", " : ""); 2615f2512ba1SRui Paulo comma = 1; 2616f2512ba1SRui Paulo } 2617281a0fd4SPatrick Kelsey if (t_flags & TF_FASTOPEN) { 2618281a0fd4SPatrick Kelsey db_printf("%sTF_FASTOPEN", comma ? ", " : ""); 2619281a0fd4SPatrick Kelsey comma = 1; 2620281a0fd4SPatrick Kelsey } 2621497057eeSRobert Watson } 2622497057eeSRobert Watson 2623497057eeSRobert Watson static void 2624497057eeSRobert Watson db_print_toobflags(char t_oobflags) 2625497057eeSRobert Watson { 2626497057eeSRobert Watson int comma; 2627497057eeSRobert Watson 2628497057eeSRobert Watson comma = 0; 2629497057eeSRobert Watson if (t_oobflags & TCPOOB_HAVEDATA) { 2630497057eeSRobert Watson db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 2631497057eeSRobert Watson comma = 1; 2632497057eeSRobert Watson } 2633497057eeSRobert Watson if (t_oobflags & TCPOOB_HADDATA) { 2634497057eeSRobert Watson db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 2635497057eeSRobert Watson comma = 1; 2636497057eeSRobert Watson } 2637497057eeSRobert Watson } 2638497057eeSRobert Watson 2639497057eeSRobert Watson static void 2640497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 2641497057eeSRobert Watson { 2642497057eeSRobert Watson 2643497057eeSRobert Watson db_print_indent(indent); 2644497057eeSRobert Watson db_printf("%s at %p\n", name, tp); 2645497057eeSRobert Watson 2646497057eeSRobert Watson indent += 2; 2647497057eeSRobert Watson 2648497057eeSRobert Watson db_print_indent(indent); 2649497057eeSRobert Watson db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 2650c28440dbSRandall Stewart TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 2651497057eeSRobert Watson 2652497057eeSRobert Watson db_print_indent(indent); 265385d94372SRobert Watson db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 2654e2f2059fSMike Silbersack &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 2655497057eeSRobert Watson 2656497057eeSRobert Watson db_print_indent(indent); 2657e2f2059fSMike Silbersack db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 2658e2f2059fSMike Silbersack &tp->t_timers->tt_delack, tp->t_inpcb); 2659497057eeSRobert Watson 2660497057eeSRobert Watson db_print_indent(indent); 2661497057eeSRobert Watson db_printf("t_state: %d (", tp->t_state); 2662497057eeSRobert Watson db_print_tstate(tp->t_state); 2663497057eeSRobert Watson db_printf(")\n"); 2664497057eeSRobert Watson 2665497057eeSRobert Watson db_print_indent(indent); 2666497057eeSRobert Watson db_printf("t_flags: 0x%x (", tp->t_flags); 2667497057eeSRobert Watson db_print_tflags(tp->t_flags); 2668497057eeSRobert Watson db_printf(")\n"); 2669497057eeSRobert Watson 2670497057eeSRobert Watson db_print_indent(indent); 2671497057eeSRobert Watson db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 2672497057eeSRobert Watson tp->snd_una, tp->snd_max, tp->snd_nxt); 2673497057eeSRobert Watson 2674497057eeSRobert Watson db_print_indent(indent); 2675497057eeSRobert Watson db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 2676497057eeSRobert Watson tp->snd_up, tp->snd_wl1, tp->snd_wl2); 2677497057eeSRobert Watson 2678497057eeSRobert Watson db_print_indent(indent); 2679497057eeSRobert Watson db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 2680497057eeSRobert Watson tp->iss, tp->irs, tp->rcv_nxt); 2681497057eeSRobert Watson 2682497057eeSRobert Watson db_print_indent(indent); 26833ac12506SJonathan T. Looney db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n", 2684497057eeSRobert Watson tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 2685497057eeSRobert Watson 2686497057eeSRobert Watson db_print_indent(indent); 26873ac12506SJonathan T. Looney db_printf("snd_wnd: %u snd_cwnd: %u\n", 26881c18314dSAndre Oppermann tp->snd_wnd, tp->snd_cwnd); 2689497057eeSRobert Watson 2690497057eeSRobert Watson db_print_indent(indent); 26913ac12506SJonathan T. Looney db_printf("snd_ssthresh: %u snd_recover: " 26921c18314dSAndre Oppermann "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 2693497057eeSRobert Watson 2694497057eeSRobert Watson db_print_indent(indent); 26950c39d38dSGleb Smirnoff db_printf("t_rcvtime: %u t_startime: %u\n", 26960c39d38dSGleb Smirnoff tp->t_rcvtime, tp->t_starttime); 2697497057eeSRobert Watson 2698497057eeSRobert Watson db_print_indent(indent); 26991c18314dSAndre Oppermann db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 27001c18314dSAndre Oppermann tp->t_rtttime, tp->t_rtseq); 2701497057eeSRobert Watson 2702497057eeSRobert Watson db_print_indent(indent); 27031c18314dSAndre Oppermann db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 27041c18314dSAndre Oppermann tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 2705497057eeSRobert Watson 2706497057eeSRobert Watson db_print_indent(indent); 2707497057eeSRobert Watson db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 2708497057eeSRobert Watson "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 2709497057eeSRobert Watson tp->t_rttbest); 2710497057eeSRobert Watson 2711497057eeSRobert Watson db_print_indent(indent); 27123ac12506SJonathan T. Looney db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n", 2713497057eeSRobert Watson tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 2714497057eeSRobert Watson 2715497057eeSRobert Watson db_print_indent(indent); 2716497057eeSRobert Watson db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 2717497057eeSRobert Watson db_print_toobflags(tp->t_oobflags); 2718497057eeSRobert Watson db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 2719497057eeSRobert Watson 2720497057eeSRobert Watson db_print_indent(indent); 2721497057eeSRobert Watson db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 2722497057eeSRobert Watson tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 2723497057eeSRobert Watson 2724497057eeSRobert Watson db_print_indent(indent); 27259f78a87aSJohn Baldwin db_printf("ts_recent: %u ts_recent_age: %u\n", 27261a553740SAndre Oppermann tp->ts_recent, tp->ts_recent_age); 2727497057eeSRobert Watson 2728497057eeSRobert Watson db_print_indent(indent); 2729497057eeSRobert Watson db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 27303ac12506SJonathan T. Looney "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 2731497057eeSRobert Watson 2732497057eeSRobert Watson db_print_indent(indent); 27333ac12506SJonathan T. Looney db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x " 27349f78a87aSJohn Baldwin "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 2735497057eeSRobert Watson tp->snd_recover_prev, tp->t_badrxtwin); 2736497057eeSRobert Watson 2737497057eeSRobert Watson db_print_indent(indent); 27383529149eSAndre Oppermann db_printf("snd_numholes: %d snd_holes first: %p\n", 27393529149eSAndre Oppermann tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 2740497057eeSRobert Watson 2741497057eeSRobert Watson db_print_indent(indent); 2742497057eeSRobert Watson db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: " 2743497057eeSRobert Watson "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata); 2744497057eeSRobert Watson 2745497057eeSRobert Watson /* Skip sackblks, sackhint. */ 2746497057eeSRobert Watson 2747497057eeSRobert Watson db_print_indent(indent); 2748497057eeSRobert Watson db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 2749497057eeSRobert Watson tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 2750497057eeSRobert Watson } 2751497057eeSRobert Watson 2752497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 2753497057eeSRobert Watson { 2754497057eeSRobert Watson struct tcpcb *tp; 2755497057eeSRobert Watson 2756497057eeSRobert Watson if (!have_addr) { 2757497057eeSRobert Watson db_printf("usage: show tcpcb <addr>\n"); 2758497057eeSRobert Watson return; 2759497057eeSRobert Watson } 2760497057eeSRobert Watson tp = (struct tcpcb *)addr; 2761497057eeSRobert Watson 2762497057eeSRobert Watson db_print_tcpcb(tp, "tcpcb", 0); 2763497057eeSRobert Watson } 2764497057eeSRobert Watson #endif 2765