1c398230bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1993 3623dce13SRobert Watson * The Regents of the University of California. 4497057eeSRobert Watson * Copyright (c) 2006-2007 Robert N. M. Watson 5623dce13SRobert Watson * All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes * 311fdbc7aeSGarrett Wollman * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 344b421e2dSMike Silbersack #include <sys/cdefs.h> 354b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 364b421e2dSMike Silbersack 37497057eeSRobert Watson #include "opt_ddb.h" 381cfd4b53SBruce M Simpson #include "opt_inet.h" 39fb59c426SYoshinobu Inoue #include "opt_inet6.h" 400cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 410cc12cc5SJoerg Wunsch 42df8bae1dSRodney W. Grimes #include <sys/param.h> 43df8bae1dSRodney W. Grimes #include <sys/systm.h> 44f76fcf6dSJeffrey Hsu #include <sys/malloc.h> 45c7a82f90SGarrett Wollman #include <sys/kernel.h> 4698163b98SPoul-Henning Kamp #include <sys/sysctl.h> 47df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 48fb59c426SYoshinobu Inoue #ifdef INET6 49fb59c426SYoshinobu Inoue #include <sys/domain.h> 50fb59c426SYoshinobu Inoue #endif /* INET6 */ 51df8bae1dSRodney W. Grimes #include <sys/socket.h> 52df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 53df8bae1dSRodney W. Grimes #include <sys/protosw.h> 5491421ba2SRobert Watson #include <sys/proc.h> 5591421ba2SRobert Watson #include <sys/jail.h> 56603724d3SBjoern A. Zeeb #include <sys/vimage.h> 57df8bae1dSRodney W. Grimes 58497057eeSRobert Watson #ifdef DDB 59497057eeSRobert Watson #include <ddb/ddb.h> 60497057eeSRobert Watson #endif 61497057eeSRobert Watson 62df8bae1dSRodney W. Grimes #include <net/if.h> 63df8bae1dSRodney W. Grimes #include <net/route.h> 64df8bae1dSRodney W. Grimes 65df8bae1dSRodney W. Grimes #include <netinet/in.h> 66df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 67fb59c426SYoshinobu Inoue #ifdef INET6 68fb59c426SYoshinobu Inoue #include <netinet/ip6.h> 69fb59c426SYoshinobu Inoue #endif 70df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 71fb59c426SYoshinobu Inoue #ifdef INET6 72fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h> 73fb59c426SYoshinobu Inoue #endif 74b5e8ce9fSBruce Evans #include <netinet/in_var.h> 75df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 76fb59c426SYoshinobu Inoue #ifdef INET6 77fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 78a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h> 79fb59c426SYoshinobu Inoue #endif 80df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 81df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 82df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 83df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 84df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 85df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 86610ee2f9SDavid Greenman #ifdef TCPDEBUG 87df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 88610ee2f9SDavid Greenman #endif 89bc65987aSKip Macy #include <netinet/tcp_offload.h> 904b79449eSBjoern A. Zeeb #include <netinet/vinet.h> 91df8bae1dSRodney W. Grimes 92df8bae1dSRodney W. Grimes /* 93df8bae1dSRodney W. Grimes * TCP protocol interface to socket abstraction. 94df8bae1dSRodney W. Grimes */ 9556dc72c3SPawel Jakub Dawidek static int tcp_attach(struct socket *); 964d77a549SAlfred Perlstein static int tcp_connect(struct tcpcb *, struct sockaddr *, 974d77a549SAlfred Perlstein struct thread *td); 98fb59c426SYoshinobu Inoue #ifdef INET6 994d77a549SAlfred Perlstein static int tcp6_connect(struct tcpcb *, struct sockaddr *, 1004d77a549SAlfred Perlstein struct thread *td); 101fb59c426SYoshinobu Inoue #endif /* INET6 */ 102623dce13SRobert Watson static void tcp_disconnect(struct tcpcb *); 103623dce13SRobert Watson static void tcp_usrclosed(struct tcpcb *); 104b8af5dfaSRobert Watson static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 1052c37256eSGarrett Wollman 1062c37256eSGarrett Wollman #ifdef TCPDEBUG 1071db24ffbSJonathan Lemon #define TCPDEBUG0 int ostate = 0 1082c37256eSGarrett Wollman #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 1094cc20ab1SSeigo Tanimura #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 1104cc20ab1SSeigo Tanimura tcp_trace(TA_USER, ostate, tp, 0, 0, req) 1112c37256eSGarrett Wollman #else 1122c37256eSGarrett Wollman #define TCPDEBUG0 1132c37256eSGarrett Wollman #define TCPDEBUG1() 1142c37256eSGarrett Wollman #define TCPDEBUG2(req) 1152c37256eSGarrett Wollman #endif 1162c37256eSGarrett Wollman 1172c37256eSGarrett Wollman /* 1182c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 1192c37256eSGarrett Wollman * and an internet control block. 1202c37256eSGarrett Wollman */ 1212c37256eSGarrett Wollman static int 122b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td) 1232c37256eSGarrett Wollman { 124f76fcf6dSJeffrey Hsu struct inpcb *inp; 125623dce13SRobert Watson struct tcpcb *tp = NULL; 126623dce13SRobert Watson int error; 1272c37256eSGarrett Wollman TCPDEBUG0; 1282c37256eSGarrett Wollman 129623dce13SRobert Watson inp = sotoinpcb(so); 130623dce13SRobert Watson KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 1312c37256eSGarrett Wollman TCPDEBUG1(); 1322c37256eSGarrett Wollman 13356dc72c3SPawel Jakub Dawidek error = tcp_attach(so); 1342c37256eSGarrett Wollman if (error) 1352c37256eSGarrett Wollman goto out; 1362c37256eSGarrett Wollman 1372c37256eSGarrett Wollman if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1383879597fSAndrey A. Chernov so->so_linger = TCP_LINGERTIME; 139f76fcf6dSJeffrey Hsu 140f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 141f76fcf6dSJeffrey Hsu tp = intotcpcb(inp); 1422c37256eSGarrett Wollman out: 1432c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 1442c37256eSGarrett Wollman return error; 1452c37256eSGarrett Wollman } 1462c37256eSGarrett Wollman 1472c37256eSGarrett Wollman /* 148a152f8a3SRobert Watson * tcp_detach is called when the socket layer loses its final reference 149a152f8a3SRobert Watson * to the socket, be it a file descriptor reference, a reference from TCP, 150a152f8a3SRobert Watson * etc. At this point, there is only one case in which we will keep around 151a152f8a3SRobert Watson * inpcb state: time wait. 152c78cbc7bSRobert Watson * 153a152f8a3SRobert Watson * This function can probably be re-absorbed back into tcp_usr_detach() now 154a152f8a3SRobert Watson * that there is a single detach path. 1552c37256eSGarrett Wollman */ 156bc725eafSRobert Watson static void 157c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp) 1582c37256eSGarrett Wollman { 1592c37256eSGarrett Wollman struct tcpcb *tp; 1608b615593SMarko Zec #ifdef INVARIANTS 1618b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 1628b615593SMarko Zec #endif 1632c37256eSGarrett Wollman 164603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1658501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 166623dce13SRobert Watson 167c78cbc7bSRobert Watson KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp")); 168c78cbc7bSRobert Watson KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so")); 169953b5606SRobert Watson 170a152f8a3SRobert Watson tp = intotcpcb(inp); 171a152f8a3SRobert Watson 172623dce13SRobert Watson if (inp->inp_vflag & INP_TIMEWAIT) { 173623dce13SRobert Watson /* 174a152f8a3SRobert Watson * There are two cases to handle: one in which the time wait 175a152f8a3SRobert Watson * state is being discarded (INP_DROPPED), and one in which 176a152f8a3SRobert Watson * this connection will remain in timewait. In the former, 177a152f8a3SRobert Watson * it is time to discard all state (except tcptw, which has 178a152f8a3SRobert Watson * already been discarded by the timewait close code, which 179a152f8a3SRobert Watson * should be further up the call stack somewhere). In the 180a152f8a3SRobert Watson * latter case, we detach from the socket, but leave the pcb 181a152f8a3SRobert Watson * present until timewait ends. 182623dce13SRobert Watson * 183a152f8a3SRobert Watson * XXXRW: Would it be cleaner to free the tcptw here? 184623dce13SRobert Watson */ 185a152f8a3SRobert Watson if (inp->inp_vflag & INP_DROPPED) { 186a152f8a3SRobert Watson KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && " 187a152f8a3SRobert Watson "INP_DROPPED && tp != NULL")); 188623dce13SRobert Watson in_pcbdetach(inp); 1890206cdb8SBjoern A. Zeeb in_pcbfree(inp); 1900206cdb8SBjoern A. Zeeb } else { 191623dce13SRobert Watson in_pcbdetach(inp); 1928501a69cSRobert Watson INP_WUNLOCK(inp); 193623dce13SRobert Watson } 194623dce13SRobert Watson } else { 195e6e65783SRobert Watson /* 196a152f8a3SRobert Watson * If the connection is not in timewait, we consider two 197a152f8a3SRobert Watson * two conditions: one in which no further processing is 198a152f8a3SRobert Watson * necessary (dropped || embryonic), and one in which TCP is 199a152f8a3SRobert Watson * not yet done, but no longer requires the socket, so the 200a152f8a3SRobert Watson * pcb will persist for the time being. 201a152f8a3SRobert Watson * 202a152f8a3SRobert Watson * XXXRW: Does the second case still occur? 203e6e65783SRobert Watson */ 204623dce13SRobert Watson if (inp->inp_vflag & INP_DROPPED || 205623dce13SRobert Watson tp->t_state < TCPS_SYN_SENT) { 206623dce13SRobert Watson tcp_discardcb(tp); 207623dce13SRobert Watson in_pcbdetach(inp); 2080206cdb8SBjoern A. Zeeb in_pcbfree(inp); 2096aee2fc5SBjoern A. Zeeb } else 210a152f8a3SRobert Watson in_pcbdetach(inp); 211623dce13SRobert Watson } 212623dce13SRobert Watson } 213c78cbc7bSRobert Watson 214c78cbc7bSRobert Watson /* 215c78cbc7bSRobert Watson * pru_detach() detaches the TCP protocol from the socket. 216c78cbc7bSRobert Watson * If the protocol state is non-embryonic, then can't 217c78cbc7bSRobert Watson * do this directly: have to initiate a pru_disconnect(), 218c78cbc7bSRobert Watson * which may finish later; embryonic TCB's can just 219c78cbc7bSRobert Watson * be discarded here. 220c78cbc7bSRobert Watson */ 221c78cbc7bSRobert Watson static void 222c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so) 223c78cbc7bSRobert Watson { 2248b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 225c78cbc7bSRobert Watson struct inpcb *inp; 226c78cbc7bSRobert Watson 227c78cbc7bSRobert Watson inp = sotoinpcb(so); 228c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL")); 229603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 2308501a69cSRobert Watson INP_WLOCK(inp); 231c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 232c78cbc7bSRobert Watson ("tcp_usr_detach: inp_socket == NULL")); 233c78cbc7bSRobert Watson tcp_detach(so, inp); 234603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2352c37256eSGarrett Wollman } 2362c37256eSGarrett Wollman 2372c37256eSGarrett Wollman /* 2382c37256eSGarrett Wollman * Give the socket an address. 2392c37256eSGarrett Wollman */ 2402c37256eSGarrett Wollman static int 241b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 2422c37256eSGarrett Wollman { 2438b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 2442c37256eSGarrett Wollman int error = 0; 245f76fcf6dSJeffrey Hsu struct inpcb *inp; 246623dce13SRobert Watson struct tcpcb *tp = NULL; 2472c37256eSGarrett Wollman struct sockaddr_in *sinp; 2482c37256eSGarrett Wollman 24952710de1SPawel Jakub Dawidek sinp = (struct sockaddr_in *)nam; 25052710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sinp)) 25152710de1SPawel Jakub Dawidek return (EINVAL); 2522c37256eSGarrett Wollman /* 2532c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 2542c37256eSGarrett Wollman * to them. 2552c37256eSGarrett Wollman */ 2562c37256eSGarrett Wollman if (sinp->sin_family == AF_INET && 25752710de1SPawel Jakub Dawidek IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 25852710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 25952710de1SPawel Jakub Dawidek 260623dce13SRobert Watson TCPDEBUG0; 261603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 262623dce13SRobert Watson inp = sotoinpcb(so); 263623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 2648501a69cSRobert Watson INP_WLOCK(inp); 265623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 266623dce13SRobert Watson error = EINVAL; 2672c37256eSGarrett Wollman goto out; 268623dce13SRobert Watson } 269623dce13SRobert Watson tp = intotcpcb(inp); 270623dce13SRobert Watson TCPDEBUG1(); 271623dce13SRobert Watson error = in_pcbbind(inp, nam, td->td_ucred); 272623dce13SRobert Watson out: 273623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 2748501a69cSRobert Watson INP_WUNLOCK(inp); 275603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 276623dce13SRobert Watson 277623dce13SRobert Watson return (error); 2782c37256eSGarrett Wollman } 2792c37256eSGarrett Wollman 280fb59c426SYoshinobu Inoue #ifdef INET6 281fb59c426SYoshinobu Inoue static int 282b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 283fb59c426SYoshinobu Inoue { 2848b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 285fb59c426SYoshinobu Inoue int error = 0; 286f76fcf6dSJeffrey Hsu struct inpcb *inp; 287623dce13SRobert Watson struct tcpcb *tp = NULL; 288fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 289fb59c426SYoshinobu Inoue 29052710de1SPawel Jakub Dawidek sin6p = (struct sockaddr_in6 *)nam; 29152710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sin6p)) 29252710de1SPawel Jakub Dawidek return (EINVAL); 293fb59c426SYoshinobu Inoue /* 294fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 295fb59c426SYoshinobu Inoue * to them. 296fb59c426SYoshinobu Inoue */ 297fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 && 29852710de1SPawel Jakub Dawidek IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 29952710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 30052710de1SPawel Jakub Dawidek 301623dce13SRobert Watson TCPDEBUG0; 302603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 303623dce13SRobert Watson inp = sotoinpcb(so); 304623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 3058501a69cSRobert Watson INP_WLOCK(inp); 306623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 307623dce13SRobert Watson error = EINVAL; 308623dce13SRobert Watson goto out; 309623dce13SRobert Watson } 310623dce13SRobert Watson tp = intotcpcb(inp); 311623dce13SRobert Watson TCPDEBUG1(); 312fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 313fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 31466ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 315fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 316fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 317fb59c426SYoshinobu Inoue else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 318fb59c426SYoshinobu Inoue struct sockaddr_in sin; 319fb59c426SYoshinobu Inoue 320fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 321fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 322fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 323b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)&sin, 324b0330ed9SPawel Jakub Dawidek td->td_ucred); 325fb59c426SYoshinobu Inoue goto out; 326fb59c426SYoshinobu Inoue } 327fb59c426SYoshinobu Inoue } 328b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, nam, td->td_ucred); 329623dce13SRobert Watson out: 330623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 3318501a69cSRobert Watson INP_WUNLOCK(inp); 332603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 333623dce13SRobert Watson return (error); 334fb59c426SYoshinobu Inoue } 335fb59c426SYoshinobu Inoue #endif /* INET6 */ 336fb59c426SYoshinobu Inoue 3372c37256eSGarrett Wollman /* 3382c37256eSGarrett Wollman * Prepare to accept connections. 3392c37256eSGarrett Wollman */ 3402c37256eSGarrett Wollman static int 341d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 3422c37256eSGarrett Wollman { 3438b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 3442c37256eSGarrett Wollman int error = 0; 345f76fcf6dSJeffrey Hsu struct inpcb *inp; 346623dce13SRobert Watson struct tcpcb *tp = NULL; 3472c37256eSGarrett Wollman 348623dce13SRobert Watson TCPDEBUG0; 349603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 350623dce13SRobert Watson inp = sotoinpcb(so); 351623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 3528501a69cSRobert Watson INP_WLOCK(inp); 353623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 354623dce13SRobert Watson error = EINVAL; 355623dce13SRobert Watson goto out; 356623dce13SRobert Watson } 357623dce13SRobert Watson tp = intotcpcb(inp); 358623dce13SRobert Watson TCPDEBUG1(); 3590daccb9cSRobert Watson SOCK_LOCK(so); 3600daccb9cSRobert Watson error = solisten_proto_check(so); 3610daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) 362b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 3630daccb9cSRobert Watson if (error == 0) { 3642c37256eSGarrett Wollman tp->t_state = TCPS_LISTEN; 365d374e81eSRobert Watson solisten_proto(so, backlog); 366bc65987aSKip Macy tcp_offload_listen_open(tp); 3670daccb9cSRobert Watson } 3680daccb9cSRobert Watson SOCK_UNLOCK(so); 369623dce13SRobert Watson 370623dce13SRobert Watson out: 371623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 3728501a69cSRobert Watson INP_WUNLOCK(inp); 373603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 374623dce13SRobert Watson return (error); 3752c37256eSGarrett Wollman } 3762c37256eSGarrett Wollman 377fb59c426SYoshinobu Inoue #ifdef INET6 378fb59c426SYoshinobu Inoue static int 379d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 380fb59c426SYoshinobu Inoue { 3818b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 382fb59c426SYoshinobu Inoue int error = 0; 383f76fcf6dSJeffrey Hsu struct inpcb *inp; 384623dce13SRobert Watson struct tcpcb *tp = NULL; 385fb59c426SYoshinobu Inoue 386623dce13SRobert Watson TCPDEBUG0; 387603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 388623dce13SRobert Watson inp = sotoinpcb(so); 389623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 3908501a69cSRobert Watson INP_WLOCK(inp); 391623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 392623dce13SRobert Watson error = EINVAL; 393623dce13SRobert Watson goto out; 394623dce13SRobert Watson } 395623dce13SRobert Watson tp = intotcpcb(inp); 396623dce13SRobert Watson TCPDEBUG1(); 3970daccb9cSRobert Watson SOCK_LOCK(so); 3980daccb9cSRobert Watson error = solisten_proto_check(so); 3990daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) { 400fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 40166ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 402fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 403b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 404fb59c426SYoshinobu Inoue } 4050daccb9cSRobert Watson if (error == 0) { 406fb59c426SYoshinobu Inoue tp->t_state = TCPS_LISTEN; 407d374e81eSRobert Watson solisten_proto(so, backlog); 4080daccb9cSRobert Watson } 4090daccb9cSRobert Watson SOCK_UNLOCK(so); 410623dce13SRobert Watson 411623dce13SRobert Watson out: 412623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 4138501a69cSRobert Watson INP_WUNLOCK(inp); 414603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 415623dce13SRobert Watson return (error); 416fb59c426SYoshinobu Inoue } 417fb59c426SYoshinobu Inoue #endif /* INET6 */ 418fb59c426SYoshinobu Inoue 4192c37256eSGarrett Wollman /* 4202c37256eSGarrett Wollman * Initiate connection to peer. 4212c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 4222c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 4232c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 4242c37256eSGarrett Wollman * Send initial segment on connection. 4252c37256eSGarrett Wollman */ 4262c37256eSGarrett Wollman static int 427b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 4282c37256eSGarrett Wollman { 4298b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 4302c37256eSGarrett Wollman int error = 0; 431f76fcf6dSJeffrey Hsu struct inpcb *inp; 432623dce13SRobert Watson struct tcpcb *tp = NULL; 4332c37256eSGarrett Wollman struct sockaddr_in *sinp; 4342c37256eSGarrett Wollman 43557bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 436e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sinp)) 437e29ef13fSDon Lewis return (EINVAL); 43852710de1SPawel Jakub Dawidek /* 43952710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 44052710de1SPawel Jakub Dawidek */ 4412c37256eSGarrett Wollman if (sinp->sin_family == AF_INET 44252710de1SPawel Jakub Dawidek && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 44352710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 444b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 445b89e82ddSJamie Gritton return (error); 44675c13541SPoul-Henning Kamp 447623dce13SRobert Watson TCPDEBUG0; 448603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 449623dce13SRobert Watson inp = sotoinpcb(so); 450623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 4518501a69cSRobert Watson INP_WLOCK(inp); 452623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 453623dce13SRobert Watson error = EINVAL; 454623dce13SRobert Watson goto out; 455623dce13SRobert Watson } 456623dce13SRobert Watson tp = intotcpcb(inp); 457623dce13SRobert Watson TCPDEBUG1(); 458b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, td)) != 0) 4592c37256eSGarrett Wollman goto out; 460bc65987aSKip Macy error = tcp_output_connect(so, nam); 461623dce13SRobert Watson out: 462623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 4638501a69cSRobert Watson INP_WUNLOCK(inp); 464603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 465623dce13SRobert Watson return (error); 4662c37256eSGarrett Wollman } 4672c37256eSGarrett Wollman 468fb59c426SYoshinobu Inoue #ifdef INET6 469fb59c426SYoshinobu Inoue static int 470b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 471fb59c426SYoshinobu Inoue { 4728b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 473fb59c426SYoshinobu Inoue int error = 0; 474f76fcf6dSJeffrey Hsu struct inpcb *inp; 475623dce13SRobert Watson struct tcpcb *tp = NULL; 476fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 477623dce13SRobert Watson 478623dce13SRobert Watson TCPDEBUG0; 479fb59c426SYoshinobu Inoue 480fb59c426SYoshinobu Inoue sin6p = (struct sockaddr_in6 *)nam; 481e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sin6p)) 482e29ef13fSDon Lewis return (EINVAL); 48352710de1SPawel Jakub Dawidek /* 48452710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 48552710de1SPawel Jakub Dawidek */ 486fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 48752710de1SPawel Jakub Dawidek && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 48852710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 489fb59c426SYoshinobu Inoue 490603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 491623dce13SRobert Watson inp = sotoinpcb(so); 492623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 4938501a69cSRobert Watson INP_WLOCK(inp); 494623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 495623dce13SRobert Watson error = EINVAL; 496623dce13SRobert Watson goto out; 497623dce13SRobert Watson } 498623dce13SRobert Watson tp = intotcpcb(inp); 499623dce13SRobert Watson TCPDEBUG1(); 50033841545SHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 501fb59c426SYoshinobu Inoue struct sockaddr_in sin; 502fb59c426SYoshinobu Inoue 503d46a5312SMaxim Konovalov if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 504d46a5312SMaxim Konovalov error = EINVAL; 505d46a5312SMaxim Konovalov goto out; 506d46a5312SMaxim Konovalov } 50733841545SHajimu UMEMOTO 508fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 509fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 510fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 511b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, 512b89e82ddSJamie Gritton &sin.sin_addr)) != 0) 513413628a7SBjoern A. Zeeb goto out; 514b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 515fb59c426SYoshinobu Inoue goto out; 516bc65987aSKip Macy error = tcp_output_connect(so, nam); 517fb59c426SYoshinobu Inoue goto out; 518fb59c426SYoshinobu Inoue } 519fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 520fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 521dcdb4371SBjoern A. Zeeb inp->inp_inc.inc_flags |= INC_ISIPV6; 522b89e82ddSJamie Gritton if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0) 523413628a7SBjoern A. Zeeb goto out; 524b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, td)) != 0) 525fb59c426SYoshinobu Inoue goto out; 526bc65987aSKip Macy error = tcp_output_connect(so, nam); 527623dce13SRobert Watson 528623dce13SRobert Watson out: 529623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 5308501a69cSRobert Watson INP_WUNLOCK(inp); 531603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 532623dce13SRobert Watson return (error); 533fb59c426SYoshinobu Inoue } 534fb59c426SYoshinobu Inoue #endif /* INET6 */ 535fb59c426SYoshinobu Inoue 5362c37256eSGarrett Wollman /* 5372c37256eSGarrett Wollman * Initiate disconnect from peer. 5382c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 5392c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 5402c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 5412c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 5422c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 5432c37256eSGarrett Wollman * when peer sends FIN and acks ours. 5442c37256eSGarrett Wollman * 5452c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 5462c37256eSGarrett Wollman */ 5472c37256eSGarrett Wollman static int 5482c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 5492c37256eSGarrett Wollman { 5508b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 551f76fcf6dSJeffrey Hsu struct inpcb *inp; 552623dce13SRobert Watson struct tcpcb *tp = NULL; 553623dce13SRobert Watson int error = 0; 5542c37256eSGarrett Wollman 555623dce13SRobert Watson TCPDEBUG0; 556603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 557623dce13SRobert Watson inp = sotoinpcb(so); 558623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 5598501a69cSRobert Watson INP_WLOCK(inp); 560623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 56121367f63SSam Leffler error = ECONNRESET; 562623dce13SRobert Watson goto out; 563623dce13SRobert Watson } 564623dce13SRobert Watson tp = intotcpcb(inp); 565623dce13SRobert Watson TCPDEBUG1(); 566623dce13SRobert Watson tcp_disconnect(tp); 567623dce13SRobert Watson out: 568623dce13SRobert Watson TCPDEBUG2(PRU_DISCONNECT); 5698501a69cSRobert Watson INP_WUNLOCK(inp); 570603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 571623dce13SRobert Watson return (error); 5722c37256eSGarrett Wollman } 5732c37256eSGarrett Wollman 5742c37256eSGarrett Wollman /* 5752c37256eSGarrett Wollman * Accept a connection. Essentially all the work is 5762c37256eSGarrett Wollman * done at higher levels; just return the address 5772c37256eSGarrett Wollman * of the peer, storing through addr. 5782c37256eSGarrett Wollman */ 5792c37256eSGarrett Wollman static int 58057bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 5812c37256eSGarrett Wollman { 5828b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 5832c37256eSGarrett Wollman int error = 0; 584f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 5851db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 58626ef6ac4SDon Lewis struct in_addr addr; 58726ef6ac4SDon Lewis in_port_t port = 0; 5881db24ffbSJonathan Lemon TCPDEBUG0; 5892c37256eSGarrett Wollman 5903d2d3ef4SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 5913d2d3ef4SRobert Watson return (ECONNABORTED); 592f76fcf6dSJeffrey Hsu 593f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 594623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 595603724d3SBjoern A. Zeeb INP_INFO_RLOCK(&V_tcbinfo); 5968501a69cSRobert Watson INP_WLOCK(inp); 597623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 5983d2d3ef4SRobert Watson error = ECONNABORTED; 599623dce13SRobert Watson goto out; 600623dce13SRobert Watson } 6011db24ffbSJonathan Lemon tp = intotcpcb(inp); 6021db24ffbSJonathan Lemon TCPDEBUG1(); 603f76fcf6dSJeffrey Hsu 604f76fcf6dSJeffrey Hsu /* 60554d642bbSRobert Watson * We inline in_getpeeraddr and COMMON_END here, so that we can 60626ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 60726ef6ac4SDon Lewis * release the lock. 608f76fcf6dSJeffrey Hsu */ 60926ef6ac4SDon Lewis port = inp->inp_fport; 61026ef6ac4SDon Lewis addr = inp->inp_faddr; 611f76fcf6dSJeffrey Hsu 612623dce13SRobert Watson out: 613623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 6148501a69cSRobert Watson INP_WUNLOCK(inp); 615603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_tcbinfo); 61626ef6ac4SDon Lewis if (error == 0) 61726ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 61826ef6ac4SDon Lewis return error; 6192c37256eSGarrett Wollman } 6202c37256eSGarrett Wollman 621fb59c426SYoshinobu Inoue #ifdef INET6 622fb59c426SYoshinobu Inoue static int 623fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 624fb59c426SYoshinobu Inoue { 625f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 626fb59c426SYoshinobu Inoue int error = 0; 6271db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 62826ef6ac4SDon Lewis struct in_addr addr; 62926ef6ac4SDon Lewis struct in6_addr addr6; 63026ef6ac4SDon Lewis in_port_t port = 0; 63126ef6ac4SDon Lewis int v4 = 0; 6321db24ffbSJonathan Lemon TCPDEBUG0; 633fb59c426SYoshinobu Inoue 634b4470c16SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 635b4470c16SRobert Watson return (ECONNABORTED); 636f76fcf6dSJeffrey Hsu 637f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 638623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 6398501a69cSRobert Watson INP_WLOCK(inp); 640623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 64121367f63SSam Leffler error = ECONNABORTED; 642623dce13SRobert Watson goto out; 643623dce13SRobert Watson } 6441db24ffbSJonathan Lemon tp = intotcpcb(inp); 6451db24ffbSJonathan Lemon TCPDEBUG1(); 646623dce13SRobert Watson 64726ef6ac4SDon Lewis /* 64826ef6ac4SDon Lewis * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 64926ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 65026ef6ac4SDon Lewis * release the lock. 65126ef6ac4SDon Lewis */ 65226ef6ac4SDon Lewis if (inp->inp_vflag & INP_IPV4) { 65326ef6ac4SDon Lewis v4 = 1; 65426ef6ac4SDon Lewis port = inp->inp_fport; 65526ef6ac4SDon Lewis addr = inp->inp_faddr; 65626ef6ac4SDon Lewis } else { 65726ef6ac4SDon Lewis port = inp->inp_fport; 65826ef6ac4SDon Lewis addr6 = inp->in6p_faddr; 65926ef6ac4SDon Lewis } 66026ef6ac4SDon Lewis 661623dce13SRobert Watson out: 662623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 6638501a69cSRobert Watson INP_WUNLOCK(inp); 66426ef6ac4SDon Lewis if (error == 0) { 66526ef6ac4SDon Lewis if (v4) 66626ef6ac4SDon Lewis *nam = in6_v4mapsin6_sockaddr(port, &addr); 66726ef6ac4SDon Lewis else 66826ef6ac4SDon Lewis *nam = in6_sockaddr(port, &addr6); 66926ef6ac4SDon Lewis } 67026ef6ac4SDon Lewis return error; 671fb59c426SYoshinobu Inoue } 672fb59c426SYoshinobu Inoue #endif /* INET6 */ 673f76fcf6dSJeffrey Hsu 674f76fcf6dSJeffrey Hsu /* 6752c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 6762c37256eSGarrett Wollman */ 6772c37256eSGarrett Wollman static int 6782c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 6792c37256eSGarrett Wollman { 6808b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 6812c37256eSGarrett Wollman int error = 0; 682f76fcf6dSJeffrey Hsu struct inpcb *inp; 683623dce13SRobert Watson struct tcpcb *tp = NULL; 6842c37256eSGarrett Wollman 685623dce13SRobert Watson TCPDEBUG0; 686603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 687623dce13SRobert Watson inp = sotoinpcb(so); 688623dce13SRobert Watson KASSERT(inp != NULL, ("inp == NULL")); 6898501a69cSRobert Watson INP_WLOCK(inp); 690623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 69121367f63SSam Leffler error = ECONNRESET; 692623dce13SRobert Watson goto out; 693623dce13SRobert Watson } 694623dce13SRobert Watson tp = intotcpcb(inp); 695623dce13SRobert Watson TCPDEBUG1(); 6962c37256eSGarrett Wollman socantsendmore(so); 697623dce13SRobert Watson tcp_usrclosed(tp); 698bc65987aSKip Macy error = tcp_output_disconnect(tp); 699623dce13SRobert Watson 700623dce13SRobert Watson out: 701623dce13SRobert Watson TCPDEBUG2(PRU_SHUTDOWN); 7028501a69cSRobert Watson INP_WUNLOCK(inp); 703603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 704623dce13SRobert Watson 705623dce13SRobert Watson return (error); 7062c37256eSGarrett Wollman } 7072c37256eSGarrett Wollman 7082c37256eSGarrett Wollman /* 7092c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 7102c37256eSGarrett Wollman */ 7112c37256eSGarrett Wollman static int 7122c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 7132c37256eSGarrett Wollman { 714f76fcf6dSJeffrey Hsu struct inpcb *inp; 715623dce13SRobert Watson struct tcpcb *tp = NULL; 716623dce13SRobert Watson int error = 0; 7172c37256eSGarrett Wollman 718623dce13SRobert Watson TCPDEBUG0; 719623dce13SRobert Watson inp = sotoinpcb(so); 720623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 7218501a69cSRobert Watson INP_WLOCK(inp); 722623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 72321367f63SSam Leffler error = ECONNRESET; 724623dce13SRobert Watson goto out; 725623dce13SRobert Watson } 726623dce13SRobert Watson tp = intotcpcb(inp); 727623dce13SRobert Watson TCPDEBUG1(); 728bc65987aSKip Macy tcp_output_rcvd(tp); 729623dce13SRobert Watson 730623dce13SRobert Watson out: 731623dce13SRobert Watson TCPDEBUG2(PRU_RCVD); 7328501a69cSRobert Watson INP_WUNLOCK(inp); 733623dce13SRobert Watson return (error); 7342c37256eSGarrett Wollman } 7352c37256eSGarrett Wollman 7362c37256eSGarrett Wollman /* 7372c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 7389c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 7399c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 7409c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 7419c9906e9SPeter Wemm * generally are caller-frees. 7422c37256eSGarrett Wollman */ 7432c37256eSGarrett Wollman static int 74457bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 745b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 7462c37256eSGarrett Wollman { 7478b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 7482c37256eSGarrett Wollman int error = 0; 749f76fcf6dSJeffrey Hsu struct inpcb *inp; 750623dce13SRobert Watson struct tcpcb *tp = NULL; 751623dce13SRobert Watson int headlocked = 0; 752fb59c426SYoshinobu Inoue #ifdef INET6 753fb59c426SYoshinobu Inoue int isipv6; 754fb59c426SYoshinobu Inoue #endif 7559c9906e9SPeter Wemm TCPDEBUG0; 7562c37256eSGarrett Wollman 757f76fcf6dSJeffrey Hsu /* 758623dce13SRobert Watson * We require the pcbinfo lock in two cases: 759623dce13SRobert Watson * 760623dce13SRobert Watson * (1) An implied connect is taking place, which can result in 761623dce13SRobert Watson * binding IPs and ports and hence modification of the pcb hash 762623dce13SRobert Watson * chains. 763623dce13SRobert Watson * 764623dce13SRobert Watson * (2) PRUS_EOF is set, resulting in explicit close on the send. 765f76fcf6dSJeffrey Hsu */ 766623dce13SRobert Watson if ((nam != NULL) || (flags & PRUS_EOF)) { 767603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 768623dce13SRobert Watson headlocked = 1; 769623dce13SRobert Watson } 770f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 771623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 7728501a69cSRobert Watson INP_WLOCK(inp); 773623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 7747ff0b850SAndre Oppermann if (control) 7757ff0b850SAndre Oppermann m_freem(control); 7767ff0b850SAndre Oppermann if (m) 7777ff0b850SAndre Oppermann m_freem(m); 77821367f63SSam Leffler error = ECONNRESET; 7799c9906e9SPeter Wemm goto out; 7809c9906e9SPeter Wemm } 781fb59c426SYoshinobu Inoue #ifdef INET6 782fb59c426SYoshinobu Inoue isipv6 = nam && nam->sa_family == AF_INET6; 783fb59c426SYoshinobu Inoue #endif /* INET6 */ 7849c9906e9SPeter Wemm tp = intotcpcb(inp); 7859c9906e9SPeter Wemm TCPDEBUG1(); 7869c9906e9SPeter Wemm if (control) { 7879c9906e9SPeter Wemm /* TCP doesn't do control messages (rights, creds, etc) */ 7889c9906e9SPeter Wemm if (control->m_len) { 7899c9906e9SPeter Wemm m_freem(control); 7902c37256eSGarrett Wollman if (m) 7912c37256eSGarrett Wollman m_freem(m); 792744f87eaSDavid Greenman error = EINVAL; 793744f87eaSDavid Greenman goto out; 7942c37256eSGarrett Wollman } 7959c9906e9SPeter Wemm m_freem(control); /* empty control, just free it */ 7969c9906e9SPeter Wemm } 7972c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 798395bb186SSam Leffler sbappendstream(&so->so_snd, m); 7992c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 8002c37256eSGarrett Wollman /* 8012c37256eSGarrett Wollman * Do implied connect if not yet connected, 8022c37256eSGarrett Wollman * initialize window to default value, and 8032c37256eSGarrett Wollman * initialize maxseg/maxopd using peer's cached 8042c37256eSGarrett Wollman * MSS. 8052c37256eSGarrett Wollman */ 806603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 807fb59c426SYoshinobu Inoue #ifdef INET6 808fb59c426SYoshinobu Inoue if (isipv6) 809b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 810fb59c426SYoshinobu Inoue else 811fb59c426SYoshinobu Inoue #endif /* INET6 */ 812b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 8132c37256eSGarrett Wollman if (error) 8142c37256eSGarrett Wollman goto out; 8152c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 8162c37256eSGarrett Wollman tcp_mss(tp, -1); 8172c37256eSGarrett Wollman } 8182c37256eSGarrett Wollman if (flags & PRUS_EOF) { 8192c37256eSGarrett Wollman /* 8202c37256eSGarrett Wollman * Close the send side of the connection after 8212c37256eSGarrett Wollman * the data is sent. 8222c37256eSGarrett Wollman */ 823603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 8242c37256eSGarrett Wollman socantsendmore(so); 825623dce13SRobert Watson tcp_usrclosed(tp); 8262c37256eSGarrett Wollman } 827623dce13SRobert Watson if (headlocked) { 828603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 829623dce13SRobert Watson headlocked = 0; 830623dce13SRobert Watson } 831b0acefa8SBill Fenner if (tp != NULL) { 832b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 833b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 834bc65987aSKip Macy error = tcp_output_send(tp); 835b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 836b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 837b0acefa8SBill Fenner } 8382c37256eSGarrett Wollman } else { 839623dce13SRobert Watson /* 840623dce13SRobert Watson * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 841623dce13SRobert Watson */ 842d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 8432c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 844d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 8452c37256eSGarrett Wollman m_freem(m); 8462c37256eSGarrett Wollman error = ENOBUFS; 8472c37256eSGarrett Wollman goto out; 8482c37256eSGarrett Wollman } 8492c37256eSGarrett Wollman /* 8502c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 8512c37256eSGarrett Wollman * the urgent pointer points to the last octet 8522c37256eSGarrett Wollman * of urgent data. We continue, however, 8532c37256eSGarrett Wollman * to consider it to indicate the first octet 8542c37256eSGarrett Wollman * of data past the urgent section. 8552c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 8562c37256eSGarrett Wollman */ 857d2bc35abSRobert Watson sbappendstream_locked(&so->so_snd, m); 858d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 859ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 860ef53690bSGarrett Wollman /* 861ef53690bSGarrett Wollman * Do implied connect if not yet connected, 862ef53690bSGarrett Wollman * initialize window to default value, and 863ef53690bSGarrett Wollman * initialize maxseg/maxopd using peer's cached 864ef53690bSGarrett Wollman * MSS. 865ef53690bSGarrett Wollman */ 866603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 867fb59c426SYoshinobu Inoue #ifdef INET6 868fb59c426SYoshinobu Inoue if (isipv6) 869b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 870fb59c426SYoshinobu Inoue else 871fb59c426SYoshinobu Inoue #endif /* INET6 */ 872b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 873ef53690bSGarrett Wollman if (error) 874ef53690bSGarrett Wollman goto out; 875ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 876ef53690bSGarrett Wollman tcp_mss(tp, -1); 877603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 878623dce13SRobert Watson headlocked = 0; 879623dce13SRobert Watson } else if (nam) { 880603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 881623dce13SRobert Watson headlocked = 0; 882623dce13SRobert Watson } 8832c37256eSGarrett Wollman tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 8842cdbfa66SPaul Saab tp->t_flags |= TF_FORCEDATA; 885bc65987aSKip Macy error = tcp_output_send(tp); 8862cdbfa66SPaul Saab tp->t_flags &= ~TF_FORCEDATA; 8872c37256eSGarrett Wollman } 888d1401c90SRobert Watson out: 889d1401c90SRobert Watson TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 8902c37256eSGarrett Wollman ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 8918501a69cSRobert Watson INP_WUNLOCK(inp); 892623dce13SRobert Watson if (headlocked) 893603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 89473fddedaSPeter Grehan return (error); 8952c37256eSGarrett Wollman } 8962c37256eSGarrett Wollman 8972c37256eSGarrett Wollman /* 898a152f8a3SRobert Watson * Abort the TCP. Drop the connection abruptly. 8992c37256eSGarrett Wollman */ 900ac45e92fSRobert Watson static void 9012c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 9022c37256eSGarrett Wollman { 9038b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 904f76fcf6dSJeffrey Hsu struct inpcb *inp; 905a152f8a3SRobert Watson struct tcpcb *tp = NULL; 906623dce13SRobert Watson TCPDEBUG0; 907c78cbc7bSRobert Watson 908ac45e92fSRobert Watson inp = sotoinpcb(so); 909c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 910c78cbc7bSRobert Watson 911603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 9128501a69cSRobert Watson INP_WLOCK(inp); 913c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 914c78cbc7bSRobert Watson ("tcp_usr_abort: inp_socket == NULL")); 915c78cbc7bSRobert Watson 916c78cbc7bSRobert Watson /* 917a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, drop. 918c78cbc7bSRobert Watson */ 919c78cbc7bSRobert Watson if (!(inp->inp_vflag & INP_TIMEWAIT) && 920c78cbc7bSRobert Watson !(inp->inp_vflag & INP_DROPPED)) { 921c78cbc7bSRobert Watson tp = intotcpcb(inp); 922a152f8a3SRobert Watson TCPDEBUG1(); 923c78cbc7bSRobert Watson tcp_drop(tp, ECONNABORTED); 924a152f8a3SRobert Watson TCPDEBUG2(PRU_ABORT); 925c78cbc7bSRobert Watson } 926a152f8a3SRobert Watson if (!(inp->inp_vflag & INP_DROPPED)) { 927a152f8a3SRobert Watson SOCK_LOCK(so); 928a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 929a152f8a3SRobert Watson SOCK_UNLOCK(so); 930a152f8a3SRobert Watson inp->inp_vflag |= INP_SOCKREF; 931a152f8a3SRobert Watson } 9328501a69cSRobert Watson INP_WUNLOCK(inp); 933603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 934a152f8a3SRobert Watson } 935a152f8a3SRobert Watson 936a152f8a3SRobert Watson /* 937a152f8a3SRobert Watson * TCP socket is closed. Start friendly disconnect. 938a152f8a3SRobert Watson */ 939a152f8a3SRobert Watson static void 940a152f8a3SRobert Watson tcp_usr_close(struct socket *so) 941a152f8a3SRobert Watson { 9428b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 943a152f8a3SRobert Watson struct inpcb *inp; 944a152f8a3SRobert Watson struct tcpcb *tp = NULL; 945a152f8a3SRobert Watson TCPDEBUG0; 946a152f8a3SRobert Watson 947a152f8a3SRobert Watson inp = sotoinpcb(so); 948a152f8a3SRobert Watson KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 949a152f8a3SRobert Watson 950603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 9518501a69cSRobert Watson INP_WLOCK(inp); 952a152f8a3SRobert Watson KASSERT(inp->inp_socket != NULL, 953a152f8a3SRobert Watson ("tcp_usr_close: inp_socket == NULL")); 954a152f8a3SRobert Watson 955a152f8a3SRobert Watson /* 956a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, initiate 957a152f8a3SRobert Watson * a disconnect. 958a152f8a3SRobert Watson */ 959a152f8a3SRobert Watson if (!(inp->inp_vflag & INP_TIMEWAIT) && 960a152f8a3SRobert Watson !(inp->inp_vflag & INP_DROPPED)) { 961a152f8a3SRobert Watson tp = intotcpcb(inp); 962a152f8a3SRobert Watson TCPDEBUG1(); 963a152f8a3SRobert Watson tcp_disconnect(tp); 964a152f8a3SRobert Watson TCPDEBUG2(PRU_CLOSE); 965a152f8a3SRobert Watson } 966a152f8a3SRobert Watson if (!(inp->inp_vflag & INP_DROPPED)) { 967a152f8a3SRobert Watson SOCK_LOCK(so); 968a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 969a152f8a3SRobert Watson SOCK_UNLOCK(so); 970a152f8a3SRobert Watson inp->inp_vflag |= INP_SOCKREF; 971a152f8a3SRobert Watson } 9728501a69cSRobert Watson INP_WUNLOCK(inp); 973603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 9742c37256eSGarrett Wollman } 9752c37256eSGarrett Wollman 9762c37256eSGarrett Wollman /* 9772c37256eSGarrett Wollman * Receive out-of-band data. 9782c37256eSGarrett Wollman */ 9792c37256eSGarrett Wollman static int 9802c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 9812c37256eSGarrett Wollman { 9822c37256eSGarrett Wollman int error = 0; 983f76fcf6dSJeffrey Hsu struct inpcb *inp; 984623dce13SRobert Watson struct tcpcb *tp = NULL; 9852c37256eSGarrett Wollman 986623dce13SRobert Watson TCPDEBUG0; 987623dce13SRobert Watson inp = sotoinpcb(so); 988623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 9898501a69cSRobert Watson INP_WLOCK(inp); 990623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 99121367f63SSam Leffler error = ECONNRESET; 992623dce13SRobert Watson goto out; 993623dce13SRobert Watson } 994623dce13SRobert Watson tp = intotcpcb(inp); 995623dce13SRobert Watson TCPDEBUG1(); 9962c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 997c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 9984cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 9994cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 10002c37256eSGarrett Wollman error = EINVAL; 10012c37256eSGarrett Wollman goto out; 10022c37256eSGarrett Wollman } 10032c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 10042c37256eSGarrett Wollman error = EWOULDBLOCK; 10052c37256eSGarrett Wollman goto out; 10062c37256eSGarrett Wollman } 10072c37256eSGarrett Wollman m->m_len = 1; 10082c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 10092c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 10102c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1011623dce13SRobert Watson 1012623dce13SRobert Watson out: 1013623dce13SRobert Watson TCPDEBUG2(PRU_RCVOOB); 10148501a69cSRobert Watson INP_WUNLOCK(inp); 1015623dce13SRobert Watson return (error); 10162c37256eSGarrett Wollman } 10172c37256eSGarrett Wollman 10182c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = { 1019756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1020756d52a1SPoul-Henning Kamp .pru_accept = tcp_usr_accept, 1021756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1022756d52a1SPoul-Henning Kamp .pru_bind = tcp_usr_bind, 1023756d52a1SPoul-Henning Kamp .pru_connect = tcp_usr_connect, 1024756d52a1SPoul-Henning Kamp .pru_control = in_control, 1025756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1026756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1027756d52a1SPoul-Henning Kamp .pru_listen = tcp_usr_listen, 102854d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1029756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1030756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1031756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 1032756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 103354d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1034a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1035a152f8a3SRobert Watson .pru_close = tcp_usr_close, 10362c37256eSGarrett Wollman }; 1037df8bae1dSRodney W. Grimes 1038fb59c426SYoshinobu Inoue #ifdef INET6 1039fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = { 1040756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1041756d52a1SPoul-Henning Kamp .pru_accept = tcp6_usr_accept, 1042756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1043756d52a1SPoul-Henning Kamp .pru_bind = tcp6_usr_bind, 1044756d52a1SPoul-Henning Kamp .pru_connect = tcp6_usr_connect, 1045756d52a1SPoul-Henning Kamp .pru_control = in6_control, 1046756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1047756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1048756d52a1SPoul-Henning Kamp .pru_listen = tcp6_usr_listen, 1049756d52a1SPoul-Henning Kamp .pru_peeraddr = in6_mapped_peeraddr, 1050756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1051756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1052756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 1053756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 1054756d52a1SPoul-Henning Kamp .pru_sockaddr = in6_mapped_sockaddr, 1055a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1056a152f8a3SRobert Watson .pru_close = tcp_usr_close, 1057fb59c426SYoshinobu Inoue }; 1058fb59c426SYoshinobu Inoue #endif /* INET6 */ 1059fb59c426SYoshinobu Inoue 1060a0292f23SGarrett Wollman /* 1061a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 1062a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 10635200e00eSIan Dowse * port number if needed. Call in_pcbconnect_setup to do the routing and 10645200e00eSIan Dowse * to choose a local host address (interface). If there is an existing 10655200e00eSIan Dowse * incarnation of the same connection in TIME-WAIT state and if the remote 10665200e00eSIan Dowse * host was sending CC options and if the connection duration was < MSL, then 1067a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 1068a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 1069a0292f23SGarrett Wollman */ 10700312fbe9SPoul-Henning Kamp static int 1071ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1072a0292f23SGarrett Wollman { 1073a0292f23SGarrett Wollman struct inpcb *inp = tp->t_inpcb, *oinp; 1074a0292f23SGarrett Wollman struct socket *so = inp->inp_socket; 10758b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 10765200e00eSIan Dowse struct in_addr laddr; 10775200e00eSIan Dowse u_short lport; 1078c3229e05SDavid Greenman int error; 1079a0292f23SGarrett Wollman 1080603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 10818501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1082623dce13SRobert Watson 1083a0292f23SGarrett Wollman if (inp->inp_lport == 0) { 1084b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1085a0292f23SGarrett Wollman if (error) 1086a0292f23SGarrett Wollman return error; 1087a0292f23SGarrett Wollman } 1088a0292f23SGarrett Wollman 1089a0292f23SGarrett Wollman /* 1090a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 1091a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 1092a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 1093a0292f23SGarrett Wollman */ 10945200e00eSIan Dowse laddr = inp->inp_laddr; 10955200e00eSIan Dowse lport = inp->inp_lport; 10965200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1097b0330ed9SPawel Jakub Dawidek &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 10985200e00eSIan Dowse if (error && oinp == NULL) 1099d3628763SRodney W. Grimes return error; 1100c94c54e4SAndre Oppermann if (oinp) 1101a0292f23SGarrett Wollman return EADDRINUSE; 11025200e00eSIan Dowse inp->inp_laddr = laddr; 110315bd2b43SDavid Greenman in_pcbrehash(inp); 1104a0292f23SGarrett Wollman 1105087b55eaSAndre Oppermann /* 1106087b55eaSAndre Oppermann * Compute window scaling to request: 1107087b55eaSAndre Oppermann * Scale to fit into sweet spot. See tcp_syncache.c. 1108087b55eaSAndre Oppermann * XXX: This should move to tcp_output(). 1109087b55eaSAndre Oppermann */ 1110a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 11119b3bc6bfSMike Silbersack (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1112a0292f23SGarrett Wollman tp->request_r_scale++; 1113a0292f23SGarrett Wollman 1114a0292f23SGarrett Wollman soisconnecting(so); 1115603724d3SBjoern A. Zeeb V_tcpstat.tcps_connattempt++; 1116a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_SENT; 1117b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepinit); 1118b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 11191fcc99b5SMatthew Dillon tp->t_bw_rtseq = tp->iss; 1120a0292f23SGarrett Wollman tcp_sendseqinit(tp); 1121a45d2726SAndras Olah 1122a0292f23SGarrett Wollman return 0; 1123a0292f23SGarrett Wollman } 1124a0292f23SGarrett Wollman 1125fb59c426SYoshinobu Inoue #ifdef INET6 1126fb59c426SYoshinobu Inoue static int 1127ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1128fb59c426SYoshinobu Inoue { 1129fb59c426SYoshinobu Inoue struct inpcb *inp = tp->t_inpcb, *oinp; 1130fb59c426SYoshinobu Inoue struct socket *so = inp->inp_socket; 11318b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 1132fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 1133fb59c426SYoshinobu Inoue struct in6_addr *addr6; 1134fb59c426SYoshinobu Inoue int error; 1135fb59c426SYoshinobu Inoue 1136603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 11378501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1138623dce13SRobert Watson 1139fb59c426SYoshinobu Inoue if (inp->inp_lport == 0) { 1140b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1141fb59c426SYoshinobu Inoue if (error) 1142fb59c426SYoshinobu Inoue return error; 1143fb59c426SYoshinobu Inoue } 1144fb59c426SYoshinobu Inoue 1145fb59c426SYoshinobu Inoue /* 1146fb59c426SYoshinobu Inoue * Cannot simply call in_pcbconnect, because there might be an 1147fb59c426SYoshinobu Inoue * earlier incarnation of this same connection still in 1148fb59c426SYoshinobu Inoue * TIME_WAIT state, creating an ADDRINUSE error. 1149a1f7e5f8SHajimu UMEMOTO * in6_pcbladdr() also handles scope zone IDs. 1150fb59c426SYoshinobu Inoue */ 1151fb59c426SYoshinobu Inoue error = in6_pcbladdr(inp, nam, &addr6); 1152fb59c426SYoshinobu Inoue if (error) 1153fb59c426SYoshinobu Inoue return error; 1154fb59c426SYoshinobu Inoue oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 1155fb59c426SYoshinobu Inoue &sin6->sin6_addr, sin6->sin6_port, 1156fb59c426SYoshinobu Inoue IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 1157fb59c426SYoshinobu Inoue ? addr6 1158fb59c426SYoshinobu Inoue : &inp->in6p_laddr, 1159fb59c426SYoshinobu Inoue inp->inp_lport, 0, NULL); 1160c94c54e4SAndre Oppermann if (oinp) 1161fb59c426SYoshinobu Inoue return EADDRINUSE; 1162fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1163fb59c426SYoshinobu Inoue inp->in6p_laddr = *addr6; 1164fb59c426SYoshinobu Inoue inp->in6p_faddr = sin6->sin6_addr; 1165fb59c426SYoshinobu Inoue inp->inp_fport = sin6->sin6_port; 11668a59da30SHajimu UMEMOTO /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 1167fc384fa5SBjoern A. Zeeb inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 1168fc384fa5SBjoern A. Zeeb if (inp->inp_flags & IN6P_AUTOFLOWLABEL) 1169fc384fa5SBjoern A. Zeeb inp->inp_flow |= 11708a59da30SHajimu UMEMOTO (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 1171fb59c426SYoshinobu Inoue in_pcbrehash(inp); 1172fb59c426SYoshinobu Inoue 1173fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1174fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1175fb59c426SYoshinobu Inoue (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 1176fb59c426SYoshinobu Inoue tp->request_r_scale++; 1177fb59c426SYoshinobu Inoue 1178fb59c426SYoshinobu Inoue soisconnecting(so); 1179603724d3SBjoern A. Zeeb V_tcpstat.tcps_connattempt++; 1180fb59c426SYoshinobu Inoue tp->t_state = TCPS_SYN_SENT; 1181b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepinit); 1182b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 11831fcc99b5SMatthew Dillon tp->t_bw_rtseq = tp->iss; 1184fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1185fb59c426SYoshinobu Inoue 1186fb59c426SYoshinobu Inoue return 0; 1187fb59c426SYoshinobu Inoue } 1188fb59c426SYoshinobu Inoue #endif /* INET6 */ 1189fb59c426SYoshinobu Inoue 1190cfe8b629SGarrett Wollman /* 1191b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1192b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1193b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1194b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1195b8af5dfaSRobert Watson * from Linux. 1196b8af5dfaSRobert Watson */ 1197b8af5dfaSRobert Watson static void 1198ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1199b8af5dfaSRobert Watson { 1200b8af5dfaSRobert Watson 12018501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1202b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1203b8af5dfaSRobert Watson 1204b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1205b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1206b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 12073529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 1208b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1209b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1210b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1211b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1212b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1213b8af5dfaSRobert Watson } 12141baaf834SBruce M Simpson 12151baaf834SBruce M Simpson ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 12161baaf834SBruce M Simpson ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 12171baaf834SBruce M Simpson 1218b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1219b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1220b8af5dfaSRobert Watson 1221b8af5dfaSRobert Watson /* 1222b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1223b8af5dfaSRobert Watson */ 1224c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1225535fbad6SKip Macy ti->tcpi_rcv_nxt = tp->rcv_nxt; 1226b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 1227b8af5dfaSRobert Watson ti->tcpi_snd_bwnd = tp->snd_bwnd; 1228535fbad6SKip Macy ti->tcpi_snd_nxt = tp->snd_nxt; 1229535fbad6SKip Macy ti->__tcpi_snd_mss = tp->t_maxseg; 1230535fbad6SKip Macy ti->__tcpi_rcv_mss = tp->t_maxseg; 1231535fbad6SKip Macy if (tp->t_flags & TF_TOE) 1232535fbad6SKip Macy ti->tcpi_options |= TCPI_OPT_TOE; 1233b8af5dfaSRobert Watson } 1234b8af5dfaSRobert Watson 1235b8af5dfaSRobert Watson /* 12361e8f5ffaSRobert Watson * tcp_ctloutput() must drop the inpcb lock before performing copyin on 12371e8f5ffaSRobert Watson * socket option arguments. When it re-acquires the lock after the copy, it 12381e8f5ffaSRobert Watson * has to revalidate that the connection is still valid for the socket 12391e8f5ffaSRobert Watson * option. 1240cfe8b629SGarrett Wollman */ 12418501a69cSRobert Watson #define INP_WLOCK_RECHECK(inp) do { \ 12428501a69cSRobert Watson INP_WLOCK(inp); \ 12431e8f5ffaSRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { \ 12448501a69cSRobert Watson INP_WUNLOCK(inp); \ 12451e8f5ffaSRobert Watson return (ECONNRESET); \ 12461e8f5ffaSRobert Watson } \ 12471e8f5ffaSRobert Watson tp = intotcpcb(inp); \ 12481e8f5ffaSRobert Watson } while(0) 12491e8f5ffaSRobert Watson 1250df8bae1dSRodney W. Grimes int 1251ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1252df8bae1dSRodney W. Grimes { 12538b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 12543f9d1ef9SRobert Watson int error, opt, optval; 1255df8bae1dSRodney W. Grimes struct inpcb *inp; 1256cfe8b629SGarrett Wollman struct tcpcb *tp; 1257b8af5dfaSRobert Watson struct tcp_info ti; 1258df8bae1dSRodney W. Grimes 1259cfe8b629SGarrett Wollman error = 0; 1260df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1261623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 12628501a69cSRobert Watson INP_WLOCK(inp); 1263cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 1264fb59c426SYoshinobu Inoue #ifdef INET6 12655cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 12668501a69cSRobert Watson INP_WUNLOCK(inp); 1267fb59c426SYoshinobu Inoue error = ip6_ctloutput(so, sopt); 12681e8f5ffaSRobert Watson } else { 1269fb59c426SYoshinobu Inoue #endif /* INET6 */ 12708501a69cSRobert Watson INP_WUNLOCK(inp); 1271cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 12721e8f5ffaSRobert Watson #ifdef INET6 12731e8f5ffaSRobert Watson } 12741e8f5ffaSRobert Watson #endif 1275df8bae1dSRodney W. Grimes return (error); 1276df8bae1dSRodney W. Grimes } 1277623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 12788501a69cSRobert Watson INP_WUNLOCK(inp); 12791e8f5ffaSRobert Watson return (ECONNRESET); 1280623dce13SRobert Watson } 1281df8bae1dSRodney W. Grimes 1282cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1283cfe8b629SGarrett Wollman case SOPT_SET: 1284cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 12851cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 128688f6b043SBruce M Simpson case TCP_MD5SIG: 12878501a69cSRobert Watson INP_WUNLOCK(inp); 12881cfd4b53SBruce M Simpson error = sooptcopyin(sopt, &optval, sizeof optval, 12891cfd4b53SBruce M Simpson sizeof optval); 12901cfd4b53SBruce M Simpson if (error) 12911e8f5ffaSRobert Watson return (error); 12921cfd4b53SBruce M Simpson 12938501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 12941cfd4b53SBruce M Simpson if (optval > 0) 12951cfd4b53SBruce M Simpson tp->t_flags |= TF_SIGNATURE; 12961cfd4b53SBruce M Simpson else 12971cfd4b53SBruce M Simpson tp->t_flags &= ~TF_SIGNATURE; 12988501a69cSRobert Watson INP_WUNLOCK(inp); 12991cfd4b53SBruce M Simpson break; 13001cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 1301df8bae1dSRodney W. Grimes case TCP_NODELAY: 1302cfe8b629SGarrett Wollman case TCP_NOOPT: 13038501a69cSRobert Watson INP_WUNLOCK(inp); 1304cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1305cfe8b629SGarrett Wollman sizeof optval); 1306cfe8b629SGarrett Wollman if (error) 13071e8f5ffaSRobert Watson return (error); 1308cfe8b629SGarrett Wollman 13098501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1310cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1311cfe8b629SGarrett Wollman case TCP_NODELAY: 1312cfe8b629SGarrett Wollman opt = TF_NODELAY; 1313cfe8b629SGarrett Wollman break; 1314cfe8b629SGarrett Wollman case TCP_NOOPT: 1315cfe8b629SGarrett Wollman opt = TF_NOOPT; 1316cfe8b629SGarrett Wollman break; 1317cfe8b629SGarrett Wollman default: 1318cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 1319cfe8b629SGarrett Wollman break; 1320cfe8b629SGarrett Wollman } 1321cfe8b629SGarrett Wollman 1322cfe8b629SGarrett Wollman if (optval) 1323cfe8b629SGarrett Wollman tp->t_flags |= opt; 1324df8bae1dSRodney W. Grimes else 1325cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 13268501a69cSRobert Watson INP_WUNLOCK(inp); 1327df8bae1dSRodney W. Grimes break; 1328df8bae1dSRodney W. Grimes 1329007581c0SJonathan Lemon case TCP_NOPUSH: 13308501a69cSRobert Watson INP_WUNLOCK(inp); 1331007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 1332007581c0SJonathan Lemon sizeof optval); 1333007581c0SJonathan Lemon if (error) 13341e8f5ffaSRobert Watson return (error); 1335007581c0SJonathan Lemon 13368501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1337007581c0SJonathan Lemon if (optval) 1338007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 1339007581c0SJonathan Lemon else { 1340007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 1341007581c0SJonathan Lemon error = tcp_output(tp); 1342007581c0SJonathan Lemon } 13438501a69cSRobert Watson INP_WUNLOCK(inp); 1344007581c0SJonathan Lemon break; 1345007581c0SJonathan Lemon 1346df8bae1dSRodney W. Grimes case TCP_MAXSEG: 13478501a69cSRobert Watson INP_WUNLOCK(inp); 1348cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1349cfe8b629SGarrett Wollman sizeof optval); 1350cfe8b629SGarrett Wollman if (error) 13511e8f5ffaSRobert Watson return (error); 1352df8bae1dSRodney W. Grimes 13538501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 135453369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 1355603724d3SBjoern A. Zeeb optval + 40 >= V_tcp_minmss) 1356cfe8b629SGarrett Wollman tp->t_maxseg = optval; 1357a0292f23SGarrett Wollman else 1358a0292f23SGarrett Wollman error = EINVAL; 13598501a69cSRobert Watson INP_WUNLOCK(inp); 1360a0292f23SGarrett Wollman break; 1361a0292f23SGarrett Wollman 1362b8af5dfaSRobert Watson case TCP_INFO: 13638501a69cSRobert Watson INP_WUNLOCK(inp); 1364b8af5dfaSRobert Watson error = EINVAL; 1365b8af5dfaSRobert Watson break; 1366b8af5dfaSRobert Watson 1367df8bae1dSRodney W. Grimes default: 13688501a69cSRobert Watson INP_WUNLOCK(inp); 1369df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1370df8bae1dSRodney W. Grimes break; 1371df8bae1dSRodney W. Grimes } 1372df8bae1dSRodney W. Grimes break; 1373df8bae1dSRodney W. Grimes 1374cfe8b629SGarrett Wollman case SOPT_GET: 13751e8f5ffaSRobert Watson tp = intotcpcb(inp); 1376cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 13771cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 137888f6b043SBruce M Simpson case TCP_MD5SIG: 13791cfd4b53SBruce M Simpson optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 13808501a69cSRobert Watson INP_WUNLOCK(inp); 1381b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 13821cfd4b53SBruce M Simpson break; 1383265ed012SBruce M Simpson #endif 13841e8f5ffaSRobert Watson 1385df8bae1dSRodney W. Grimes case TCP_NODELAY: 1386cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 13878501a69cSRobert Watson INP_WUNLOCK(inp); 1388b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1389df8bae1dSRodney W. Grimes break; 1390df8bae1dSRodney W. Grimes case TCP_MAXSEG: 1391cfe8b629SGarrett Wollman optval = tp->t_maxseg; 13928501a69cSRobert Watson INP_WUNLOCK(inp); 1393b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1394df8bae1dSRodney W. Grimes break; 1395a0292f23SGarrett Wollman case TCP_NOOPT: 1396cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 13978501a69cSRobert Watson INP_WUNLOCK(inp); 1398b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1399a0292f23SGarrett Wollman break; 1400a0292f23SGarrett Wollman case TCP_NOPUSH: 1401cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 14028501a69cSRobert Watson INP_WUNLOCK(inp); 1403b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1404b8af5dfaSRobert Watson break; 1405b8af5dfaSRobert Watson case TCP_INFO: 1406b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 14078501a69cSRobert Watson INP_WUNLOCK(inp); 1408b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 1409a0292f23SGarrett Wollman break; 1410df8bae1dSRodney W. Grimes default: 14118501a69cSRobert Watson INP_WUNLOCK(inp); 1412df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1413df8bae1dSRodney W. Grimes break; 1414df8bae1dSRodney W. Grimes } 1415df8bae1dSRodney W. Grimes break; 1416df8bae1dSRodney W. Grimes } 1417df8bae1dSRodney W. Grimes return (error); 1418df8bae1dSRodney W. Grimes } 14198501a69cSRobert Watson #undef INP_WLOCK_RECHECK 1420df8bae1dSRodney W. Grimes 142126e30fbbSDavid Greenman /* 142226e30fbbSDavid Greenman * tcp_sendspace and tcp_recvspace are the default send and receive window 142326e30fbbSDavid Greenman * sizes, respectively. These are obsolescent (this information should 142426e30fbbSDavid Greenman * be set by the route). 142526e30fbbSDavid Greenman */ 142681e561cdSDavid E. O'Brien u_long tcp_sendspace = 1024*32; 1427e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 14283d177f46SBill Fumerola &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 142981e561cdSDavid E. O'Brien u_long tcp_recvspace = 1024*64; 1430e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 14313d177f46SBill Fumerola &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1432df8bae1dSRodney W. Grimes 1433df8bae1dSRodney W. Grimes /* 1434df8bae1dSRodney W. Grimes * Attach TCP protocol to socket, allocating 1435df8bae1dSRodney W. Grimes * internet protocol control block, tcp control block, 1436df8bae1dSRodney W. Grimes * bufer space, and entering LISTEN state if to accept connections. 1437df8bae1dSRodney W. Grimes */ 14380312fbe9SPoul-Henning Kamp static int 1439ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so) 1440df8bae1dSRodney W. Grimes { 14418b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 1442ad3f9ab3SAndre Oppermann struct tcpcb *tp; 1443df8bae1dSRodney W. Grimes struct inpcb *inp; 1444df8bae1dSRodney W. Grimes int error; 1445df8bae1dSRodney W. Grimes 1446df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1447df8bae1dSRodney W. Grimes error = soreserve(so, tcp_sendspace, tcp_recvspace); 1448df8bae1dSRodney W. Grimes if (error) 1449df8bae1dSRodney W. Grimes return (error); 1450df8bae1dSRodney W. Grimes } 14516741ecf5SAndre Oppermann so->so_rcv.sb_flags |= SB_AUTOSIZE; 14526741ecf5SAndre Oppermann so->so_snd.sb_flags |= SB_AUTOSIZE; 1453603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 1454603724d3SBjoern A. Zeeb error = in_pcballoc(so, &V_tcbinfo); 1455f2de87feSRobert Watson if (error) { 1456603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 1457df8bae1dSRodney W. Grimes return (error); 1458f2de87feSRobert Watson } 1459df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1460fb59c426SYoshinobu Inoue #ifdef INET6 14615cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 1462fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 1463fb59c426SYoshinobu Inoue inp->in6p_hops = -1; /* use kernel default */ 1464fb59c426SYoshinobu Inoue } 1465fb59c426SYoshinobu Inoue else 1466fb59c426SYoshinobu Inoue #endif 1467cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1468df8bae1dSRodney W. Grimes tp = tcp_newtcpcb(inp); 1469623dce13SRobert Watson if (tp == NULL) { 1470df8bae1dSRodney W. Grimes in_pcbdetach(inp); 14710206cdb8SBjoern A. Zeeb in_pcbfree(inp); 1472603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 1473df8bae1dSRodney W. Grimes return (ENOBUFS); 1474df8bae1dSRodney W. Grimes } 1475df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 14768501a69cSRobert Watson INP_WUNLOCK(inp); 1477603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 1478df8bae1dSRodney W. Grimes return (0); 1479df8bae1dSRodney W. Grimes } 1480df8bae1dSRodney W. Grimes 1481df8bae1dSRodney W. Grimes /* 1482df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 1483df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 1484df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 1485df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 1486df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 1487df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 1488df8bae1dSRodney W. Grimes */ 1489623dce13SRobert Watson static void 1490ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp) 1491df8bae1dSRodney W. Grimes { 1492e6e0b5ffSRobert Watson struct inpcb *inp = tp->t_inpcb; 1493e6e0b5ffSRobert Watson struct socket *so = inp->inp_socket; 14948b615593SMarko Zec #ifdef INVARIANTS 14958b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 14968b615593SMarko Zec #endif 1497e6e0b5ffSRobert Watson 1498603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 14998501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1500df8bae1dSRodney W. Grimes 1501623dce13SRobert Watson /* 1502623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 1503623dce13SRobert Watson * socket is still open. 1504623dce13SRobert Watson */ 1505623dce13SRobert Watson if (tp->t_state < TCPS_ESTABLISHED) { 1506df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1507623dce13SRobert Watson KASSERT(tp != NULL, 1508623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 1509623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 1510243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 1511623dce13SRobert Watson KASSERT(tp != NULL, 1512623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 1513623dce13SRobert Watson } else { 1514df8bae1dSRodney W. Grimes soisdisconnecting(so); 1515df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 1516623dce13SRobert Watson tcp_usrclosed(tp); 1517953b5606SRobert Watson if (!(inp->inp_vflag & INP_DROPPED)) 1518bc65987aSKip Macy tcp_output_disconnect(tp); 1519df8bae1dSRodney W. Grimes } 1520df8bae1dSRodney W. Grimes } 1521df8bae1dSRodney W. Grimes 1522df8bae1dSRodney W. Grimes /* 1523df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 1524df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 1525df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1526df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 1527df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 1528df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 1529df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 1530df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 1531df8bae1dSRodney W. Grimes */ 1532623dce13SRobert Watson static void 1533ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp) 1534df8bae1dSRodney W. Grimes { 15358b615593SMarko Zec #ifdef INVARIANTS 15368b615593SMarko Zec INIT_VNET_INET(tp->t_inpcb->inp_vnet); 15378b615593SMarko Zec #endif 1538df8bae1dSRodney W. Grimes 1539603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 15408501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1541e6e0b5ffSRobert Watson 1542df8bae1dSRodney W. Grimes switch (tp->t_state) { 1543df8bae1dSRodney W. Grimes case TCPS_LISTEN: 1544bc65987aSKip Macy tcp_offload_listen_close(tp); 1545bc65987aSKip Macy /* FALLTHROUGH */ 1546bc65987aSKip Macy case TCPS_CLOSED: 1547df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 1548df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1549623dce13SRobert Watson /* 1550623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 1551623dce13SRobert Watson * still open. 1552623dce13SRobert Watson */ 1553623dce13SRobert Watson KASSERT(tp != NULL, 1554623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 1555df8bae1dSRodney W. Grimes break; 1556df8bae1dSRodney W. Grimes 1557a0292f23SGarrett Wollman case TCPS_SYN_SENT: 1558df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1559a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 1560a0292f23SGarrett Wollman break; 1561a0292f23SGarrett Wollman 1562df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1563df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_1; 1564df8bae1dSRodney W. Grimes break; 1565df8bae1dSRodney W. Grimes 1566df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1567df8bae1dSRodney W. Grimes tp->t_state = TCPS_LAST_ACK; 1568df8bae1dSRodney W. Grimes break; 1569df8bae1dSRodney W. Grimes } 1570abc7d910SRobert Watson if (tp->t_state >= TCPS_FIN_WAIT_2) { 1571df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 1572abc7d910SRobert Watson /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 15737c72af87SMohan Srinivasan if (tp->t_state == TCPS_FIN_WAIT_2) { 15747c72af87SMohan Srinivasan int timeout; 15757c72af87SMohan Srinivasan 15767c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 15777c72af87SMohan Srinivasan tcp_finwait2_timeout : tcp_maxidle; 1578b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 1579b6239c4aSAndras Olah } 1580df8bae1dSRodney W. Grimes } 15817c72af87SMohan Srinivasan } 1582497057eeSRobert Watson 1583497057eeSRobert Watson #ifdef DDB 1584497057eeSRobert Watson static void 1585497057eeSRobert Watson db_print_indent(int indent) 1586497057eeSRobert Watson { 1587497057eeSRobert Watson int i; 1588497057eeSRobert Watson 1589497057eeSRobert Watson for (i = 0; i < indent; i++) 1590497057eeSRobert Watson db_printf(" "); 1591497057eeSRobert Watson } 1592497057eeSRobert Watson 1593497057eeSRobert Watson static void 1594497057eeSRobert Watson db_print_tstate(int t_state) 1595497057eeSRobert Watson { 1596497057eeSRobert Watson 1597497057eeSRobert Watson switch (t_state) { 1598497057eeSRobert Watson case TCPS_CLOSED: 1599497057eeSRobert Watson db_printf("TCPS_CLOSED"); 1600497057eeSRobert Watson return; 1601497057eeSRobert Watson 1602497057eeSRobert Watson case TCPS_LISTEN: 1603497057eeSRobert Watson db_printf("TCPS_LISTEN"); 1604497057eeSRobert Watson return; 1605497057eeSRobert Watson 1606497057eeSRobert Watson case TCPS_SYN_SENT: 1607497057eeSRobert Watson db_printf("TCPS_SYN_SENT"); 1608497057eeSRobert Watson return; 1609497057eeSRobert Watson 1610497057eeSRobert Watson case TCPS_SYN_RECEIVED: 1611497057eeSRobert Watson db_printf("TCPS_SYN_RECEIVED"); 1612497057eeSRobert Watson return; 1613497057eeSRobert Watson 1614497057eeSRobert Watson case TCPS_ESTABLISHED: 1615497057eeSRobert Watson db_printf("TCPS_ESTABLISHED"); 1616497057eeSRobert Watson return; 1617497057eeSRobert Watson 1618497057eeSRobert Watson case TCPS_CLOSE_WAIT: 1619497057eeSRobert Watson db_printf("TCPS_CLOSE_WAIT"); 1620497057eeSRobert Watson return; 1621497057eeSRobert Watson 1622497057eeSRobert Watson case TCPS_FIN_WAIT_1: 1623497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_1"); 1624497057eeSRobert Watson return; 1625497057eeSRobert Watson 1626497057eeSRobert Watson case TCPS_CLOSING: 1627497057eeSRobert Watson db_printf("TCPS_CLOSING"); 1628497057eeSRobert Watson return; 1629497057eeSRobert Watson 1630497057eeSRobert Watson case TCPS_LAST_ACK: 1631497057eeSRobert Watson db_printf("TCPS_LAST_ACK"); 1632497057eeSRobert Watson return; 1633497057eeSRobert Watson 1634497057eeSRobert Watson case TCPS_FIN_WAIT_2: 1635497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_2"); 1636497057eeSRobert Watson return; 1637497057eeSRobert Watson 1638497057eeSRobert Watson case TCPS_TIME_WAIT: 1639497057eeSRobert Watson db_printf("TCPS_TIME_WAIT"); 1640497057eeSRobert Watson return; 1641497057eeSRobert Watson 1642497057eeSRobert Watson default: 1643497057eeSRobert Watson db_printf("unknown"); 1644497057eeSRobert Watson return; 1645497057eeSRobert Watson } 1646497057eeSRobert Watson } 1647497057eeSRobert Watson 1648497057eeSRobert Watson static void 1649497057eeSRobert Watson db_print_tflags(u_int t_flags) 1650497057eeSRobert Watson { 1651497057eeSRobert Watson int comma; 1652497057eeSRobert Watson 1653497057eeSRobert Watson comma = 0; 1654497057eeSRobert Watson if (t_flags & TF_ACKNOW) { 1655497057eeSRobert Watson db_printf("%sTF_ACKNOW", comma ? ", " : ""); 1656497057eeSRobert Watson comma = 1; 1657497057eeSRobert Watson } 1658497057eeSRobert Watson if (t_flags & TF_DELACK) { 1659497057eeSRobert Watson db_printf("%sTF_DELACK", comma ? ", " : ""); 1660497057eeSRobert Watson comma = 1; 1661497057eeSRobert Watson } 1662497057eeSRobert Watson if (t_flags & TF_NODELAY) { 1663497057eeSRobert Watson db_printf("%sTF_NODELAY", comma ? ", " : ""); 1664497057eeSRobert Watson comma = 1; 1665497057eeSRobert Watson } 1666497057eeSRobert Watson if (t_flags & TF_NOOPT) { 1667497057eeSRobert Watson db_printf("%sTF_NOOPT", comma ? ", " : ""); 1668497057eeSRobert Watson comma = 1; 1669497057eeSRobert Watson } 1670497057eeSRobert Watson if (t_flags & TF_SENTFIN) { 1671497057eeSRobert Watson db_printf("%sTF_SENTFIN", comma ? ", " : ""); 1672497057eeSRobert Watson comma = 1; 1673497057eeSRobert Watson } 1674497057eeSRobert Watson if (t_flags & TF_REQ_SCALE) { 1675497057eeSRobert Watson db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 1676497057eeSRobert Watson comma = 1; 1677497057eeSRobert Watson } 1678497057eeSRobert Watson if (t_flags & TF_RCVD_SCALE) { 1679497057eeSRobert Watson db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 1680497057eeSRobert Watson comma = 1; 1681497057eeSRobert Watson } 1682497057eeSRobert Watson if (t_flags & TF_REQ_TSTMP) { 1683497057eeSRobert Watson db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 1684497057eeSRobert Watson comma = 1; 1685497057eeSRobert Watson } 1686497057eeSRobert Watson if (t_flags & TF_RCVD_TSTMP) { 1687497057eeSRobert Watson db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 1688497057eeSRobert Watson comma = 1; 1689497057eeSRobert Watson } 1690497057eeSRobert Watson if (t_flags & TF_SACK_PERMIT) { 1691497057eeSRobert Watson db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 1692497057eeSRobert Watson comma = 1; 1693497057eeSRobert Watson } 1694497057eeSRobert Watson if (t_flags & TF_NEEDSYN) { 1695497057eeSRobert Watson db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 1696497057eeSRobert Watson comma = 1; 1697497057eeSRobert Watson } 1698497057eeSRobert Watson if (t_flags & TF_NEEDFIN) { 1699497057eeSRobert Watson db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 1700497057eeSRobert Watson comma = 1; 1701497057eeSRobert Watson } 1702497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 1703497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 1704497057eeSRobert Watson comma = 1; 1705497057eeSRobert Watson } 1706497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 1707497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 1708497057eeSRobert Watson comma = 1; 1709497057eeSRobert Watson } 1710497057eeSRobert Watson if (t_flags & TF_MORETOCOME) { 1711497057eeSRobert Watson db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 1712497057eeSRobert Watson comma = 1; 1713497057eeSRobert Watson } 1714497057eeSRobert Watson if (t_flags & TF_LQ_OVERFLOW) { 1715497057eeSRobert Watson db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 1716497057eeSRobert Watson comma = 1; 1717497057eeSRobert Watson } 1718497057eeSRobert Watson if (t_flags & TF_LASTIDLE) { 1719497057eeSRobert Watson db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 1720497057eeSRobert Watson comma = 1; 1721497057eeSRobert Watson } 1722497057eeSRobert Watson if (t_flags & TF_RXWIN0SENT) { 1723497057eeSRobert Watson db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 1724497057eeSRobert Watson comma = 1; 1725497057eeSRobert Watson } 1726497057eeSRobert Watson if (t_flags & TF_FASTRECOVERY) { 1727497057eeSRobert Watson db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 1728497057eeSRobert Watson comma = 1; 1729497057eeSRobert Watson } 1730497057eeSRobert Watson if (t_flags & TF_WASFRECOVERY) { 1731497057eeSRobert Watson db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 1732497057eeSRobert Watson comma = 1; 1733497057eeSRobert Watson } 1734497057eeSRobert Watson if (t_flags & TF_SIGNATURE) { 1735497057eeSRobert Watson db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 1736497057eeSRobert Watson comma = 1; 1737497057eeSRobert Watson } 1738497057eeSRobert Watson if (t_flags & TF_FORCEDATA) { 1739497057eeSRobert Watson db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 1740497057eeSRobert Watson comma = 1; 1741497057eeSRobert Watson } 1742497057eeSRobert Watson if (t_flags & TF_TSO) { 1743497057eeSRobert Watson db_printf("%sTF_TSO", comma ? ", " : ""); 1744497057eeSRobert Watson comma = 1; 1745497057eeSRobert Watson } 1746f2512ba1SRui Paulo if (t_flags & TF_ECN_PERMIT) { 1747f2512ba1SRui Paulo db_printf("%sTF_ECN_PERMIT", comma ? ", " : ""); 1748f2512ba1SRui Paulo comma = 1; 1749f2512ba1SRui Paulo } 1750497057eeSRobert Watson } 1751497057eeSRobert Watson 1752497057eeSRobert Watson static void 1753497057eeSRobert Watson db_print_toobflags(char t_oobflags) 1754497057eeSRobert Watson { 1755497057eeSRobert Watson int comma; 1756497057eeSRobert Watson 1757497057eeSRobert Watson comma = 0; 1758497057eeSRobert Watson if (t_oobflags & TCPOOB_HAVEDATA) { 1759497057eeSRobert Watson db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 1760497057eeSRobert Watson comma = 1; 1761497057eeSRobert Watson } 1762497057eeSRobert Watson if (t_oobflags & TCPOOB_HADDATA) { 1763497057eeSRobert Watson db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 1764497057eeSRobert Watson comma = 1; 1765497057eeSRobert Watson } 1766497057eeSRobert Watson } 1767497057eeSRobert Watson 1768497057eeSRobert Watson static void 1769497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 1770497057eeSRobert Watson { 1771497057eeSRobert Watson 1772497057eeSRobert Watson db_print_indent(indent); 1773497057eeSRobert Watson db_printf("%s at %p\n", name, tp); 1774497057eeSRobert Watson 1775497057eeSRobert Watson indent += 2; 1776497057eeSRobert Watson 1777497057eeSRobert Watson db_print_indent(indent); 1778497057eeSRobert Watson db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 1779497057eeSRobert Watson LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 1780497057eeSRobert Watson 1781497057eeSRobert Watson db_print_indent(indent); 178285d94372SRobert Watson db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 1783e2f2059fSMike Silbersack &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 1784497057eeSRobert Watson 1785497057eeSRobert Watson db_print_indent(indent); 1786e2f2059fSMike Silbersack db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 1787e2f2059fSMike Silbersack &tp->t_timers->tt_delack, tp->t_inpcb); 1788497057eeSRobert Watson 1789497057eeSRobert Watson db_print_indent(indent); 1790497057eeSRobert Watson db_printf("t_state: %d (", tp->t_state); 1791497057eeSRobert Watson db_print_tstate(tp->t_state); 1792497057eeSRobert Watson db_printf(")\n"); 1793497057eeSRobert Watson 1794497057eeSRobert Watson db_print_indent(indent); 1795497057eeSRobert Watson db_printf("t_flags: 0x%x (", tp->t_flags); 1796497057eeSRobert Watson db_print_tflags(tp->t_flags); 1797497057eeSRobert Watson db_printf(")\n"); 1798497057eeSRobert Watson 1799497057eeSRobert Watson db_print_indent(indent); 1800497057eeSRobert Watson db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 1801497057eeSRobert Watson tp->snd_una, tp->snd_max, tp->snd_nxt); 1802497057eeSRobert Watson 1803497057eeSRobert Watson db_print_indent(indent); 1804497057eeSRobert Watson db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 1805497057eeSRobert Watson tp->snd_up, tp->snd_wl1, tp->snd_wl2); 1806497057eeSRobert Watson 1807497057eeSRobert Watson db_print_indent(indent); 1808497057eeSRobert Watson db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 1809497057eeSRobert Watson tp->iss, tp->irs, tp->rcv_nxt); 1810497057eeSRobert Watson 1811497057eeSRobert Watson db_print_indent(indent); 1812497057eeSRobert Watson db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n", 1813497057eeSRobert Watson tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 1814497057eeSRobert Watson 1815497057eeSRobert Watson db_print_indent(indent); 1816497057eeSRobert Watson db_printf("snd_wnd: %lu snd_cwnd: %lu snd_bwnd: %lu\n", 1817497057eeSRobert Watson tp->snd_wnd, tp->snd_cwnd, tp->snd_bwnd); 1818497057eeSRobert Watson 1819497057eeSRobert Watson db_print_indent(indent); 1820497057eeSRobert Watson db_printf("snd_ssthresh: %lu snd_bandwidth: %lu snd_recover: " 1821497057eeSRobert Watson "0x%08x\n", tp->snd_ssthresh, tp->snd_bandwidth, 1822497057eeSRobert Watson tp->snd_recover); 1823497057eeSRobert Watson 1824497057eeSRobert Watson db_print_indent(indent); 1825497057eeSRobert Watson db_printf("t_maxopd: %u t_rcvtime: %lu t_startime: %lu\n", 1826497057eeSRobert Watson tp->t_maxopd, tp->t_rcvtime, tp->t_starttime); 1827497057eeSRobert Watson 1828497057eeSRobert Watson db_print_indent(indent); 1829497057eeSRobert Watson db_printf("t_rttime: %d t_rtsq: 0x%08x t_bw_rtttime: %d\n", 1830497057eeSRobert Watson tp->t_rtttime, tp->t_rtseq, tp->t_bw_rtttime); 1831497057eeSRobert Watson 1832497057eeSRobert Watson db_print_indent(indent); 1833497057eeSRobert Watson db_printf("t_bw_rtseq: 0x%08x t_rxtcur: %d t_maxseg: %u " 1834497057eeSRobert Watson "t_srtt: %d\n", tp->t_bw_rtseq, tp->t_rxtcur, tp->t_maxseg, 1835497057eeSRobert Watson tp->t_srtt); 1836497057eeSRobert Watson 1837497057eeSRobert Watson db_print_indent(indent); 1838497057eeSRobert Watson db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 1839497057eeSRobert Watson "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 1840497057eeSRobert Watson tp->t_rttbest); 1841497057eeSRobert Watson 1842497057eeSRobert Watson db_print_indent(indent); 1843497057eeSRobert Watson db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n", 1844497057eeSRobert Watson tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 1845497057eeSRobert Watson 1846497057eeSRobert Watson db_print_indent(indent); 1847497057eeSRobert Watson db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 1848497057eeSRobert Watson db_print_toobflags(tp->t_oobflags); 1849497057eeSRobert Watson db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 1850497057eeSRobert Watson 1851497057eeSRobert Watson db_print_indent(indent); 1852497057eeSRobert Watson db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 1853497057eeSRobert Watson tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 1854497057eeSRobert Watson 1855497057eeSRobert Watson db_print_indent(indent); 18561a553740SAndre Oppermann db_printf("ts_recent: %u ts_recent_age: %lu\n", 18571a553740SAndre Oppermann tp->ts_recent, tp->ts_recent_age); 1858497057eeSRobert Watson 1859497057eeSRobert Watson db_print_indent(indent); 1860497057eeSRobert Watson db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 1861497057eeSRobert Watson "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 1862497057eeSRobert Watson 1863497057eeSRobert Watson db_print_indent(indent); 1864497057eeSRobert Watson db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x " 1865497057eeSRobert Watson "t_badrxtwin: %lu\n", tp->snd_ssthresh_prev, 1866497057eeSRobert Watson tp->snd_recover_prev, tp->t_badrxtwin); 1867497057eeSRobert Watson 1868497057eeSRobert Watson db_print_indent(indent); 18693529149eSAndre Oppermann db_printf("snd_numholes: %d snd_holes first: %p\n", 18703529149eSAndre Oppermann tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 1871497057eeSRobert Watson 1872497057eeSRobert Watson db_print_indent(indent); 1873497057eeSRobert Watson db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: " 1874497057eeSRobert Watson "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata); 1875497057eeSRobert Watson 1876497057eeSRobert Watson /* Skip sackblks, sackhint. */ 1877497057eeSRobert Watson 1878497057eeSRobert Watson db_print_indent(indent); 1879497057eeSRobert Watson db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 1880497057eeSRobert Watson tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 1881497057eeSRobert Watson } 1882497057eeSRobert Watson 1883497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 1884497057eeSRobert Watson { 1885497057eeSRobert Watson struct tcpcb *tp; 1886497057eeSRobert Watson 1887497057eeSRobert Watson if (!have_addr) { 1888497057eeSRobert Watson db_printf("usage: show tcpcb <addr>\n"); 1889497057eeSRobert Watson return; 1890497057eeSRobert Watson } 1891497057eeSRobert Watson tp = (struct tcpcb *)addr; 1892497057eeSRobert Watson 1893497057eeSRobert Watson db_print_tcpcb(tp, "tcpcb", 0); 1894497057eeSRobert Watson } 1895497057eeSRobert Watson #endif 1896