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> 56df8bae1dSRodney W. Grimes 57497057eeSRobert Watson #ifdef DDB 58497057eeSRobert Watson #include <ddb/ddb.h> 59497057eeSRobert Watson #endif 60497057eeSRobert Watson 61df8bae1dSRodney W. Grimes #include <net/if.h> 62df8bae1dSRodney W. Grimes #include <net/route.h> 63530c0060SRobert Watson #include <net/vnet.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> 90df8bae1dSRodney W. Grimes 91df8bae1dSRodney W. Grimes /* 92df8bae1dSRodney W. Grimes * TCP protocol interface to socket abstraction. 93df8bae1dSRodney W. Grimes */ 9456dc72c3SPawel Jakub Dawidek static int tcp_attach(struct socket *); 954d77a549SAlfred Perlstein static int tcp_connect(struct tcpcb *, struct sockaddr *, 964d77a549SAlfred Perlstein struct thread *td); 97fb59c426SYoshinobu Inoue #ifdef INET6 984d77a549SAlfred Perlstein static int tcp6_connect(struct tcpcb *, struct sockaddr *, 994d77a549SAlfred Perlstein struct thread *td); 100fb59c426SYoshinobu Inoue #endif /* INET6 */ 101623dce13SRobert Watson static void tcp_disconnect(struct tcpcb *); 102623dce13SRobert Watson static void tcp_usrclosed(struct tcpcb *); 103b8af5dfaSRobert Watson static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 1042c37256eSGarrett Wollman 1052c37256eSGarrett Wollman #ifdef TCPDEBUG 1061db24ffbSJonathan Lemon #define TCPDEBUG0 int ostate = 0 1072c37256eSGarrett Wollman #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 1084cc20ab1SSeigo Tanimura #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 1094cc20ab1SSeigo Tanimura tcp_trace(TA_USER, ostate, tp, 0, 0, req) 1102c37256eSGarrett Wollman #else 1112c37256eSGarrett Wollman #define TCPDEBUG0 1122c37256eSGarrett Wollman #define TCPDEBUG1() 1132c37256eSGarrett Wollman #define TCPDEBUG2(req) 1142c37256eSGarrett Wollman #endif 1152c37256eSGarrett Wollman 1162c37256eSGarrett Wollman /* 1172c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 1182c37256eSGarrett Wollman * and an internet control block. 1192c37256eSGarrett Wollman */ 1202c37256eSGarrett Wollman static int 121b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td) 1222c37256eSGarrett Wollman { 123f76fcf6dSJeffrey Hsu struct inpcb *inp; 124623dce13SRobert Watson struct tcpcb *tp = NULL; 125623dce13SRobert Watson int error; 1262c37256eSGarrett Wollman TCPDEBUG0; 1272c37256eSGarrett Wollman 128623dce13SRobert Watson inp = sotoinpcb(so); 129623dce13SRobert Watson KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 1302c37256eSGarrett Wollman TCPDEBUG1(); 1312c37256eSGarrett Wollman 13256dc72c3SPawel Jakub Dawidek error = tcp_attach(so); 1332c37256eSGarrett Wollman if (error) 1342c37256eSGarrett Wollman goto out; 1352c37256eSGarrett Wollman 1362c37256eSGarrett Wollman if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1373879597fSAndrey A. Chernov so->so_linger = TCP_LINGERTIME; 138f76fcf6dSJeffrey Hsu 139f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 140f76fcf6dSJeffrey Hsu tp = intotcpcb(inp); 1412c37256eSGarrett Wollman out: 1422c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 1432c37256eSGarrett Wollman return error; 1442c37256eSGarrett Wollman } 1452c37256eSGarrett Wollman 1462c37256eSGarrett Wollman /* 147a152f8a3SRobert Watson * tcp_detach is called when the socket layer loses its final reference 148a152f8a3SRobert Watson * to the socket, be it a file descriptor reference, a reference from TCP, 149a152f8a3SRobert Watson * etc. At this point, there is only one case in which we will keep around 150a152f8a3SRobert Watson * inpcb state: time wait. 151c78cbc7bSRobert Watson * 152a152f8a3SRobert Watson * This function can probably be re-absorbed back into tcp_usr_detach() now 153a152f8a3SRobert Watson * that there is a single detach path. 1542c37256eSGarrett Wollman */ 155bc725eafSRobert Watson static void 156c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp) 1572c37256eSGarrett Wollman { 1582c37256eSGarrett Wollman struct tcpcb *tp; 1592c37256eSGarrett Wollman 160603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1618501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 162623dce13SRobert Watson 163c78cbc7bSRobert Watson KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp")); 164c78cbc7bSRobert Watson KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so")); 165953b5606SRobert Watson 166a152f8a3SRobert Watson tp = intotcpcb(inp); 167a152f8a3SRobert Watson 168ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 169623dce13SRobert Watson /* 170a152f8a3SRobert Watson * There are two cases to handle: one in which the time wait 171a152f8a3SRobert Watson * state is being discarded (INP_DROPPED), and one in which 172a152f8a3SRobert Watson * this connection will remain in timewait. In the former, 173a152f8a3SRobert Watson * it is time to discard all state (except tcptw, which has 174a152f8a3SRobert Watson * already been discarded by the timewait close code, which 175a152f8a3SRobert Watson * should be further up the call stack somewhere). In the 176a152f8a3SRobert Watson * latter case, we detach from the socket, but leave the pcb 177a152f8a3SRobert Watson * present until timewait ends. 178623dce13SRobert Watson * 179a152f8a3SRobert Watson * XXXRW: Would it be cleaner to free the tcptw here? 180623dce13SRobert Watson */ 181ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED) { 182a152f8a3SRobert Watson KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && " 183a152f8a3SRobert Watson "INP_DROPPED && tp != NULL")); 184623dce13SRobert Watson in_pcbdetach(inp); 1850206cdb8SBjoern A. Zeeb in_pcbfree(inp); 1860206cdb8SBjoern A. Zeeb } else { 187623dce13SRobert Watson in_pcbdetach(inp); 1888501a69cSRobert Watson INP_WUNLOCK(inp); 189623dce13SRobert Watson } 190623dce13SRobert Watson } else { 191e6e65783SRobert Watson /* 192a152f8a3SRobert Watson * If the connection is not in timewait, we consider two 193a152f8a3SRobert Watson * two conditions: one in which no further processing is 194a152f8a3SRobert Watson * necessary (dropped || embryonic), and one in which TCP is 195a152f8a3SRobert Watson * not yet done, but no longer requires the socket, so the 196a152f8a3SRobert Watson * pcb will persist for the time being. 197a152f8a3SRobert Watson * 198a152f8a3SRobert Watson * XXXRW: Does the second case still occur? 199e6e65783SRobert Watson */ 200ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED || 201623dce13SRobert Watson tp->t_state < TCPS_SYN_SENT) { 202623dce13SRobert Watson tcp_discardcb(tp); 203623dce13SRobert Watson in_pcbdetach(inp); 2040206cdb8SBjoern A. Zeeb in_pcbfree(inp); 2056aee2fc5SBjoern A. Zeeb } else 206a152f8a3SRobert Watson in_pcbdetach(inp); 207623dce13SRobert Watson } 208623dce13SRobert Watson } 209c78cbc7bSRobert Watson 210c78cbc7bSRobert Watson /* 211c78cbc7bSRobert Watson * pru_detach() detaches the TCP protocol from the socket. 212c78cbc7bSRobert Watson * If the protocol state is non-embryonic, then can't 213c78cbc7bSRobert Watson * do this directly: have to initiate a pru_disconnect(), 214c78cbc7bSRobert Watson * which may finish later; embryonic TCB's can just 215c78cbc7bSRobert Watson * be discarded here. 216c78cbc7bSRobert Watson */ 217c78cbc7bSRobert Watson static void 218c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so) 219c78cbc7bSRobert Watson { 220c78cbc7bSRobert Watson struct inpcb *inp; 221c78cbc7bSRobert Watson 222c78cbc7bSRobert Watson inp = sotoinpcb(so); 223c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL")); 224603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 2258501a69cSRobert Watson INP_WLOCK(inp); 226c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 227c78cbc7bSRobert Watson ("tcp_usr_detach: inp_socket == NULL")); 228c78cbc7bSRobert Watson tcp_detach(so, inp); 229603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2302c37256eSGarrett Wollman } 2312c37256eSGarrett Wollman 2322c37256eSGarrett Wollman /* 2332c37256eSGarrett Wollman * Give the socket an address. 2342c37256eSGarrett Wollman */ 2352c37256eSGarrett Wollman static int 236b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 2372c37256eSGarrett Wollman { 2382c37256eSGarrett Wollman int error = 0; 239f76fcf6dSJeffrey Hsu struct inpcb *inp; 240623dce13SRobert Watson struct tcpcb *tp = NULL; 2412c37256eSGarrett Wollman struct sockaddr_in *sinp; 2422c37256eSGarrett Wollman 24352710de1SPawel Jakub Dawidek sinp = (struct sockaddr_in *)nam; 24452710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sinp)) 24552710de1SPawel Jakub Dawidek return (EINVAL); 2462c37256eSGarrett Wollman /* 2472c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 2482c37256eSGarrett Wollman * to them. 2492c37256eSGarrett Wollman */ 2502c37256eSGarrett Wollman if (sinp->sin_family == AF_INET && 25152710de1SPawel Jakub Dawidek IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 25252710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 25352710de1SPawel Jakub Dawidek 254623dce13SRobert Watson TCPDEBUG0; 255603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 256623dce13SRobert Watson inp = sotoinpcb(so); 257623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 2588501a69cSRobert Watson INP_WLOCK(inp); 259ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 260623dce13SRobert Watson error = EINVAL; 2612c37256eSGarrett Wollman goto out; 262623dce13SRobert Watson } 263623dce13SRobert Watson tp = intotcpcb(inp); 264623dce13SRobert Watson TCPDEBUG1(); 265623dce13SRobert Watson error = in_pcbbind(inp, nam, td->td_ucred); 266623dce13SRobert Watson out: 267623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 2688501a69cSRobert Watson INP_WUNLOCK(inp); 269603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 270623dce13SRobert Watson 271623dce13SRobert Watson return (error); 2722c37256eSGarrett Wollman } 2732c37256eSGarrett Wollman 274fb59c426SYoshinobu Inoue #ifdef INET6 275fb59c426SYoshinobu Inoue static int 276b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 277fb59c426SYoshinobu Inoue { 278fb59c426SYoshinobu Inoue int error = 0; 279f76fcf6dSJeffrey Hsu struct inpcb *inp; 280623dce13SRobert Watson struct tcpcb *tp = NULL; 281fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 282fb59c426SYoshinobu Inoue 28352710de1SPawel Jakub Dawidek sin6p = (struct sockaddr_in6 *)nam; 28452710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sin6p)) 28552710de1SPawel Jakub Dawidek return (EINVAL); 286fb59c426SYoshinobu Inoue /* 287fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 288fb59c426SYoshinobu Inoue * to them. 289fb59c426SYoshinobu Inoue */ 290fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 && 29152710de1SPawel Jakub Dawidek IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 29252710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 29352710de1SPawel Jakub Dawidek 294623dce13SRobert Watson TCPDEBUG0; 295603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 296623dce13SRobert Watson inp = sotoinpcb(so); 297623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 2988501a69cSRobert Watson INP_WLOCK(inp); 299ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 300623dce13SRobert Watson error = EINVAL; 301623dce13SRobert Watson goto out; 302623dce13SRobert Watson } 303623dce13SRobert Watson tp = intotcpcb(inp); 304623dce13SRobert Watson TCPDEBUG1(); 305fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 306fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 30766ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 308fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 309fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 310fb59c426SYoshinobu Inoue else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 311fb59c426SYoshinobu Inoue struct sockaddr_in sin; 312fb59c426SYoshinobu Inoue 313fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 314fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 315fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 316b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)&sin, 317b0330ed9SPawel Jakub Dawidek td->td_ucred); 318fb59c426SYoshinobu Inoue goto out; 319fb59c426SYoshinobu Inoue } 320fb59c426SYoshinobu Inoue } 321b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, nam, td->td_ucred); 322623dce13SRobert Watson out: 323623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 3248501a69cSRobert Watson INP_WUNLOCK(inp); 325603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 326623dce13SRobert Watson return (error); 327fb59c426SYoshinobu Inoue } 328fb59c426SYoshinobu Inoue #endif /* INET6 */ 329fb59c426SYoshinobu Inoue 3302c37256eSGarrett Wollman /* 3312c37256eSGarrett Wollman * Prepare to accept connections. 3322c37256eSGarrett Wollman */ 3332c37256eSGarrett Wollman static int 334d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 3352c37256eSGarrett Wollman { 3362c37256eSGarrett Wollman int error = 0; 337f76fcf6dSJeffrey Hsu struct inpcb *inp; 338623dce13SRobert Watson struct tcpcb *tp = NULL; 3392c37256eSGarrett Wollman 340623dce13SRobert Watson TCPDEBUG0; 341603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 342623dce13SRobert Watson inp = sotoinpcb(so); 343623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 3448501a69cSRobert Watson INP_WLOCK(inp); 345ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 346623dce13SRobert Watson error = EINVAL; 347623dce13SRobert Watson goto out; 348623dce13SRobert Watson } 349623dce13SRobert Watson tp = intotcpcb(inp); 350623dce13SRobert Watson TCPDEBUG1(); 3510daccb9cSRobert Watson SOCK_LOCK(so); 3520daccb9cSRobert Watson error = solisten_proto_check(so); 3530daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) 354b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 3550daccb9cSRobert Watson if (error == 0) { 3562c37256eSGarrett Wollman tp->t_state = TCPS_LISTEN; 357d374e81eSRobert Watson solisten_proto(so, backlog); 358bc65987aSKip Macy tcp_offload_listen_open(tp); 3590daccb9cSRobert Watson } 3600daccb9cSRobert Watson SOCK_UNLOCK(so); 361623dce13SRobert Watson 362623dce13SRobert Watson out: 363623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 3648501a69cSRobert Watson INP_WUNLOCK(inp); 365603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 366623dce13SRobert Watson return (error); 3672c37256eSGarrett Wollman } 3682c37256eSGarrett Wollman 369fb59c426SYoshinobu Inoue #ifdef INET6 370fb59c426SYoshinobu Inoue static int 371d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 372fb59c426SYoshinobu Inoue { 373fb59c426SYoshinobu Inoue int error = 0; 374f76fcf6dSJeffrey Hsu struct inpcb *inp; 375623dce13SRobert Watson struct tcpcb *tp = NULL; 376fb59c426SYoshinobu Inoue 377623dce13SRobert Watson TCPDEBUG0; 378603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 379623dce13SRobert Watson inp = sotoinpcb(so); 380623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 3818501a69cSRobert Watson INP_WLOCK(inp); 382ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 383623dce13SRobert Watson error = EINVAL; 384623dce13SRobert Watson goto out; 385623dce13SRobert Watson } 386623dce13SRobert Watson tp = intotcpcb(inp); 387623dce13SRobert Watson TCPDEBUG1(); 3880daccb9cSRobert Watson SOCK_LOCK(so); 3890daccb9cSRobert Watson error = solisten_proto_check(so); 3900daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) { 391fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 39266ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 393fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 394b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 395fb59c426SYoshinobu Inoue } 3960daccb9cSRobert Watson if (error == 0) { 397fb59c426SYoshinobu Inoue tp->t_state = TCPS_LISTEN; 398d374e81eSRobert Watson solisten_proto(so, backlog); 3990daccb9cSRobert Watson } 4000daccb9cSRobert Watson SOCK_UNLOCK(so); 401623dce13SRobert Watson 402623dce13SRobert Watson out: 403623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 4048501a69cSRobert Watson INP_WUNLOCK(inp); 405603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 406623dce13SRobert Watson return (error); 407fb59c426SYoshinobu Inoue } 408fb59c426SYoshinobu Inoue #endif /* INET6 */ 409fb59c426SYoshinobu Inoue 4102c37256eSGarrett Wollman /* 4112c37256eSGarrett Wollman * Initiate connection to peer. 4122c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 4132c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 4142c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 4152c37256eSGarrett Wollman * Send initial segment on connection. 4162c37256eSGarrett Wollman */ 4172c37256eSGarrett Wollman static int 418b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 4192c37256eSGarrett Wollman { 4202c37256eSGarrett Wollman int error = 0; 421f76fcf6dSJeffrey Hsu struct inpcb *inp; 422623dce13SRobert Watson struct tcpcb *tp = NULL; 4232c37256eSGarrett Wollman struct sockaddr_in *sinp; 4242c37256eSGarrett Wollman 42557bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 426e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sinp)) 427e29ef13fSDon Lewis return (EINVAL); 42852710de1SPawel Jakub Dawidek /* 42952710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 43052710de1SPawel Jakub Dawidek */ 4312c37256eSGarrett Wollman if (sinp->sin_family == AF_INET 43252710de1SPawel Jakub Dawidek && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 43352710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 434b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 435b89e82ddSJamie Gritton return (error); 43675c13541SPoul-Henning Kamp 437623dce13SRobert Watson TCPDEBUG0; 438603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 439623dce13SRobert Watson inp = sotoinpcb(so); 440623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 4418501a69cSRobert Watson INP_WLOCK(inp); 442ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 443623dce13SRobert Watson error = EINVAL; 444623dce13SRobert Watson goto out; 445623dce13SRobert Watson } 446623dce13SRobert Watson tp = intotcpcb(inp); 447623dce13SRobert Watson TCPDEBUG1(); 448b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, td)) != 0) 4492c37256eSGarrett Wollman goto out; 450bc65987aSKip Macy error = tcp_output_connect(so, nam); 451623dce13SRobert Watson out: 452623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 4538501a69cSRobert Watson INP_WUNLOCK(inp); 454603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 455623dce13SRobert Watson return (error); 4562c37256eSGarrett Wollman } 4572c37256eSGarrett Wollman 458fb59c426SYoshinobu Inoue #ifdef INET6 459fb59c426SYoshinobu Inoue static int 460b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 461fb59c426SYoshinobu Inoue { 462fb59c426SYoshinobu Inoue int error = 0; 463f76fcf6dSJeffrey Hsu struct inpcb *inp; 464623dce13SRobert Watson struct tcpcb *tp = NULL; 465fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 466623dce13SRobert Watson 467623dce13SRobert Watson TCPDEBUG0; 468fb59c426SYoshinobu Inoue 469fb59c426SYoshinobu Inoue sin6p = (struct sockaddr_in6 *)nam; 470e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sin6p)) 471e29ef13fSDon Lewis return (EINVAL); 47252710de1SPawel Jakub Dawidek /* 47352710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 47452710de1SPawel Jakub Dawidek */ 475fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 47652710de1SPawel Jakub Dawidek && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 47752710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 478fb59c426SYoshinobu Inoue 479603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 480623dce13SRobert Watson inp = sotoinpcb(so); 481623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 4828501a69cSRobert Watson INP_WLOCK(inp); 483ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 484623dce13SRobert Watson error = EINVAL; 485623dce13SRobert Watson goto out; 486623dce13SRobert Watson } 487623dce13SRobert Watson tp = intotcpcb(inp); 488623dce13SRobert Watson TCPDEBUG1(); 48933841545SHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 490fb59c426SYoshinobu Inoue struct sockaddr_in sin; 491fb59c426SYoshinobu Inoue 492d46a5312SMaxim Konovalov if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 493d46a5312SMaxim Konovalov error = EINVAL; 494d46a5312SMaxim Konovalov goto out; 495d46a5312SMaxim Konovalov } 49633841545SHajimu UMEMOTO 497fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 498fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 499fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 500b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, 501b89e82ddSJamie Gritton &sin.sin_addr)) != 0) 502413628a7SBjoern A. Zeeb goto out; 503b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 504fb59c426SYoshinobu Inoue goto out; 505bc65987aSKip Macy error = tcp_output_connect(so, nam); 506fb59c426SYoshinobu Inoue goto out; 507fb59c426SYoshinobu Inoue } 508fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 509fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 510dcdb4371SBjoern A. Zeeb inp->inp_inc.inc_flags |= INC_ISIPV6; 511b89e82ddSJamie Gritton if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0) 512413628a7SBjoern A. Zeeb goto out; 513b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, td)) != 0) 514fb59c426SYoshinobu Inoue goto out; 515bc65987aSKip Macy error = tcp_output_connect(so, nam); 516623dce13SRobert Watson 517623dce13SRobert Watson out: 518623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 5198501a69cSRobert Watson INP_WUNLOCK(inp); 520603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 521623dce13SRobert Watson return (error); 522fb59c426SYoshinobu Inoue } 523fb59c426SYoshinobu Inoue #endif /* INET6 */ 524fb59c426SYoshinobu Inoue 5252c37256eSGarrett Wollman /* 5262c37256eSGarrett Wollman * Initiate disconnect from peer. 5272c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 5282c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 5292c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 5302c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 5312c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 5322c37256eSGarrett Wollman * when peer sends FIN and acks ours. 5332c37256eSGarrett Wollman * 5342c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 5352c37256eSGarrett Wollman */ 5362c37256eSGarrett Wollman static int 5372c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 5382c37256eSGarrett Wollman { 539f76fcf6dSJeffrey Hsu struct inpcb *inp; 540623dce13SRobert Watson struct tcpcb *tp = NULL; 541623dce13SRobert Watson int error = 0; 5422c37256eSGarrett Wollman 543623dce13SRobert Watson TCPDEBUG0; 544603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 545623dce13SRobert Watson inp = sotoinpcb(so); 546623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 5478501a69cSRobert Watson INP_WLOCK(inp); 548ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 54921367f63SSam Leffler error = ECONNRESET; 550623dce13SRobert Watson goto out; 551623dce13SRobert Watson } 552623dce13SRobert Watson tp = intotcpcb(inp); 553623dce13SRobert Watson TCPDEBUG1(); 554623dce13SRobert Watson tcp_disconnect(tp); 555623dce13SRobert Watson out: 556623dce13SRobert Watson TCPDEBUG2(PRU_DISCONNECT); 5578501a69cSRobert Watson INP_WUNLOCK(inp); 558603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 559623dce13SRobert Watson return (error); 5602c37256eSGarrett Wollman } 5612c37256eSGarrett Wollman 5622c37256eSGarrett Wollman /* 5638296cddfSRobert Watson * Accept a connection. Essentially all the work is done at higher levels; 5648296cddfSRobert Watson * just return the address of the peer, storing through addr. 5658296cddfSRobert Watson * 5668296cddfSRobert Watson * The rationale for acquiring the tcbinfo lock here is somewhat complicated, 5678296cddfSRobert Watson * and is described in detail in the commit log entry for r175612. Acquiring 5688296cddfSRobert Watson * it delays an accept(2) racing with sonewconn(), which inserts the socket 5698296cddfSRobert Watson * before the inpcb address/port fields are initialized. A better fix would 5708296cddfSRobert Watson * prevent the socket from being placed in the listen queue until all fields 5718296cddfSRobert Watson * are fully initialized. 5722c37256eSGarrett Wollman */ 5732c37256eSGarrett Wollman static int 57457bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 5752c37256eSGarrett Wollman { 5762c37256eSGarrett Wollman int error = 0; 577f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 5781db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 57926ef6ac4SDon Lewis struct in_addr addr; 58026ef6ac4SDon Lewis in_port_t port = 0; 5811db24ffbSJonathan Lemon TCPDEBUG0; 5822c37256eSGarrett Wollman 5833d2d3ef4SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 5843d2d3ef4SRobert Watson return (ECONNABORTED); 585f76fcf6dSJeffrey Hsu 586f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 587623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 588603724d3SBjoern A. Zeeb INP_INFO_RLOCK(&V_tcbinfo); 5898501a69cSRobert Watson INP_WLOCK(inp); 590ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 5913d2d3ef4SRobert Watson error = ECONNABORTED; 592623dce13SRobert Watson goto out; 593623dce13SRobert Watson } 5941db24ffbSJonathan Lemon tp = intotcpcb(inp); 5951db24ffbSJonathan Lemon TCPDEBUG1(); 596f76fcf6dSJeffrey Hsu 597f76fcf6dSJeffrey Hsu /* 59854d642bbSRobert Watson * We inline in_getpeeraddr and COMMON_END here, so that we can 59926ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 60026ef6ac4SDon Lewis * release the lock. 601f76fcf6dSJeffrey Hsu */ 60226ef6ac4SDon Lewis port = inp->inp_fport; 60326ef6ac4SDon Lewis addr = inp->inp_faddr; 604f76fcf6dSJeffrey Hsu 605623dce13SRobert Watson out: 606623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 6078501a69cSRobert Watson INP_WUNLOCK(inp); 608603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_tcbinfo); 60926ef6ac4SDon Lewis if (error == 0) 61026ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 61126ef6ac4SDon Lewis return error; 6122c37256eSGarrett Wollman } 6132c37256eSGarrett Wollman 614fb59c426SYoshinobu Inoue #ifdef INET6 615fb59c426SYoshinobu Inoue static int 616fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 617fb59c426SYoshinobu Inoue { 618f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 619fb59c426SYoshinobu Inoue int error = 0; 6201db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 62126ef6ac4SDon Lewis struct in_addr addr; 62226ef6ac4SDon Lewis struct in6_addr addr6; 62326ef6ac4SDon Lewis in_port_t port = 0; 62426ef6ac4SDon Lewis int v4 = 0; 6251db24ffbSJonathan Lemon TCPDEBUG0; 626fb59c426SYoshinobu Inoue 627b4470c16SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 628b4470c16SRobert Watson return (ECONNABORTED); 629f76fcf6dSJeffrey Hsu 630f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 631623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 6328501a69cSRobert Watson INP_WLOCK(inp); 633ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 63421367f63SSam Leffler error = ECONNABORTED; 635623dce13SRobert Watson goto out; 636623dce13SRobert Watson } 6371db24ffbSJonathan Lemon tp = intotcpcb(inp); 6381db24ffbSJonathan Lemon TCPDEBUG1(); 639623dce13SRobert Watson 64026ef6ac4SDon Lewis /* 64126ef6ac4SDon Lewis * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 64226ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 64326ef6ac4SDon Lewis * release the lock. 64426ef6ac4SDon Lewis */ 64526ef6ac4SDon Lewis if (inp->inp_vflag & INP_IPV4) { 64626ef6ac4SDon Lewis v4 = 1; 64726ef6ac4SDon Lewis port = inp->inp_fport; 64826ef6ac4SDon Lewis addr = inp->inp_faddr; 64926ef6ac4SDon Lewis } else { 65026ef6ac4SDon Lewis port = inp->inp_fport; 65126ef6ac4SDon Lewis addr6 = inp->in6p_faddr; 65226ef6ac4SDon Lewis } 65326ef6ac4SDon Lewis 654623dce13SRobert Watson out: 655623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 6568501a69cSRobert Watson INP_WUNLOCK(inp); 65726ef6ac4SDon Lewis if (error == 0) { 65826ef6ac4SDon Lewis if (v4) 65926ef6ac4SDon Lewis *nam = in6_v4mapsin6_sockaddr(port, &addr); 66026ef6ac4SDon Lewis else 66126ef6ac4SDon Lewis *nam = in6_sockaddr(port, &addr6); 66226ef6ac4SDon Lewis } 66326ef6ac4SDon Lewis return error; 664fb59c426SYoshinobu Inoue } 665fb59c426SYoshinobu Inoue #endif /* INET6 */ 666f76fcf6dSJeffrey Hsu 667f76fcf6dSJeffrey Hsu /* 6682c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 6692c37256eSGarrett Wollman */ 6702c37256eSGarrett Wollman static int 6712c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 6722c37256eSGarrett Wollman { 6732c37256eSGarrett Wollman int error = 0; 674f76fcf6dSJeffrey Hsu struct inpcb *inp; 675623dce13SRobert Watson struct tcpcb *tp = NULL; 6762c37256eSGarrett Wollman 677623dce13SRobert Watson TCPDEBUG0; 678603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 679623dce13SRobert Watson inp = sotoinpcb(so); 680623dce13SRobert Watson KASSERT(inp != NULL, ("inp == NULL")); 6818501a69cSRobert Watson INP_WLOCK(inp); 682ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 68321367f63SSam Leffler error = ECONNRESET; 684623dce13SRobert Watson goto out; 685623dce13SRobert Watson } 686623dce13SRobert Watson tp = intotcpcb(inp); 687623dce13SRobert Watson TCPDEBUG1(); 6882c37256eSGarrett Wollman socantsendmore(so); 689623dce13SRobert Watson tcp_usrclosed(tp); 690ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 691bc65987aSKip Macy error = tcp_output_disconnect(tp); 692623dce13SRobert Watson 693623dce13SRobert Watson out: 694623dce13SRobert Watson TCPDEBUG2(PRU_SHUTDOWN); 6958501a69cSRobert Watson INP_WUNLOCK(inp); 696603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 697623dce13SRobert Watson 698623dce13SRobert Watson return (error); 6992c37256eSGarrett Wollman } 7002c37256eSGarrett Wollman 7012c37256eSGarrett Wollman /* 7022c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 7032c37256eSGarrett Wollman */ 7042c37256eSGarrett Wollman static int 7052c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 7062c37256eSGarrett Wollman { 707f76fcf6dSJeffrey Hsu struct inpcb *inp; 708623dce13SRobert Watson struct tcpcb *tp = NULL; 709623dce13SRobert Watson int error = 0; 7102c37256eSGarrett Wollman 711623dce13SRobert Watson TCPDEBUG0; 712623dce13SRobert Watson inp = sotoinpcb(so); 713623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 7148501a69cSRobert Watson INP_WLOCK(inp); 715ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 71621367f63SSam Leffler error = ECONNRESET; 717623dce13SRobert Watson goto out; 718623dce13SRobert Watson } 719623dce13SRobert Watson tp = intotcpcb(inp); 720623dce13SRobert Watson TCPDEBUG1(); 721bc65987aSKip Macy tcp_output_rcvd(tp); 722623dce13SRobert Watson 723623dce13SRobert Watson out: 724623dce13SRobert Watson TCPDEBUG2(PRU_RCVD); 7258501a69cSRobert Watson INP_WUNLOCK(inp); 726623dce13SRobert Watson return (error); 7272c37256eSGarrett Wollman } 7282c37256eSGarrett Wollman 7292c37256eSGarrett Wollman /* 7302c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 7319c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 7329c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 7339c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 7349c9906e9SPeter Wemm * generally are caller-frees. 7352c37256eSGarrett Wollman */ 7362c37256eSGarrett Wollman static int 73757bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 738b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 7392c37256eSGarrett Wollman { 7402c37256eSGarrett Wollman int error = 0; 741f76fcf6dSJeffrey Hsu struct inpcb *inp; 742623dce13SRobert Watson struct tcpcb *tp = NULL; 743623dce13SRobert Watson int headlocked = 0; 744fb59c426SYoshinobu Inoue #ifdef INET6 745fb59c426SYoshinobu Inoue int isipv6; 746fb59c426SYoshinobu Inoue #endif 7479c9906e9SPeter Wemm TCPDEBUG0; 7482c37256eSGarrett Wollman 749f76fcf6dSJeffrey Hsu /* 750623dce13SRobert Watson * We require the pcbinfo lock in two cases: 751623dce13SRobert Watson * 752623dce13SRobert Watson * (1) An implied connect is taking place, which can result in 753623dce13SRobert Watson * binding IPs and ports and hence modification of the pcb hash 754623dce13SRobert Watson * chains. 755623dce13SRobert Watson * 756623dce13SRobert Watson * (2) PRUS_EOF is set, resulting in explicit close on the send. 757f76fcf6dSJeffrey Hsu */ 758623dce13SRobert Watson if ((nam != NULL) || (flags & PRUS_EOF)) { 759603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 760623dce13SRobert Watson headlocked = 1; 761623dce13SRobert Watson } 762f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 763623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 7648501a69cSRobert Watson INP_WLOCK(inp); 765ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 7667ff0b850SAndre Oppermann if (control) 7677ff0b850SAndre Oppermann m_freem(control); 7687ff0b850SAndre Oppermann if (m) 7697ff0b850SAndre Oppermann m_freem(m); 77021367f63SSam Leffler error = ECONNRESET; 7719c9906e9SPeter Wemm goto out; 7729c9906e9SPeter Wemm } 773fb59c426SYoshinobu Inoue #ifdef INET6 774fb59c426SYoshinobu Inoue isipv6 = nam && nam->sa_family == AF_INET6; 775fb59c426SYoshinobu Inoue #endif /* INET6 */ 7769c9906e9SPeter Wemm tp = intotcpcb(inp); 7779c9906e9SPeter Wemm TCPDEBUG1(); 7789c9906e9SPeter Wemm if (control) { 7799c9906e9SPeter Wemm /* TCP doesn't do control messages (rights, creds, etc) */ 7809c9906e9SPeter Wemm if (control->m_len) { 7819c9906e9SPeter Wemm m_freem(control); 7822c37256eSGarrett Wollman if (m) 7832c37256eSGarrett Wollman m_freem(m); 784744f87eaSDavid Greenman error = EINVAL; 785744f87eaSDavid Greenman goto out; 7862c37256eSGarrett Wollman } 7879c9906e9SPeter Wemm m_freem(control); /* empty control, just free it */ 7889c9906e9SPeter Wemm } 7892c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 790395bb186SSam Leffler sbappendstream(&so->so_snd, m); 7912c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 7922c37256eSGarrett Wollman /* 7932c37256eSGarrett Wollman * Do implied connect if not yet connected, 7942c37256eSGarrett Wollman * initialize window to default value, and 7952c37256eSGarrett Wollman * initialize maxseg/maxopd using peer's cached 7962c37256eSGarrett Wollman * MSS. 7972c37256eSGarrett Wollman */ 798603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 799fb59c426SYoshinobu Inoue #ifdef INET6 800fb59c426SYoshinobu Inoue if (isipv6) 801b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 802fb59c426SYoshinobu Inoue else 803fb59c426SYoshinobu Inoue #endif /* INET6 */ 804b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 8052c37256eSGarrett Wollman if (error) 8062c37256eSGarrett Wollman goto out; 8072c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 8082c37256eSGarrett Wollman tcp_mss(tp, -1); 8092c37256eSGarrett Wollman } 8102c37256eSGarrett Wollman if (flags & PRUS_EOF) { 8112c37256eSGarrett Wollman /* 8122c37256eSGarrett Wollman * Close the send side of the connection after 8132c37256eSGarrett Wollman * the data is sent. 8142c37256eSGarrett Wollman */ 815603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 8162c37256eSGarrett Wollman socantsendmore(so); 817623dce13SRobert Watson tcp_usrclosed(tp); 8182c37256eSGarrett Wollman } 819623dce13SRobert Watson if (headlocked) { 820603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 821623dce13SRobert Watson headlocked = 0; 822623dce13SRobert Watson } 823ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 824b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 825b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 826bc65987aSKip Macy error = tcp_output_send(tp); 827b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 828b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 829b0acefa8SBill Fenner } 8302c37256eSGarrett Wollman } else { 831623dce13SRobert Watson /* 832623dce13SRobert Watson * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 833623dce13SRobert Watson */ 834d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 8352c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 836d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 8372c37256eSGarrett Wollman m_freem(m); 8382c37256eSGarrett Wollman error = ENOBUFS; 8392c37256eSGarrett Wollman goto out; 8402c37256eSGarrett Wollman } 8412c37256eSGarrett Wollman /* 8422c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 8432c37256eSGarrett Wollman * the urgent pointer points to the last octet 8442c37256eSGarrett Wollman * of urgent data. We continue, however, 8452c37256eSGarrett Wollman * to consider it to indicate the first octet 8462c37256eSGarrett Wollman * of data past the urgent section. 8472c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 8482c37256eSGarrett Wollman */ 849d2bc35abSRobert Watson sbappendstream_locked(&so->so_snd, m); 850d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 851ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 852ef53690bSGarrett Wollman /* 853ef53690bSGarrett Wollman * Do implied connect if not yet connected, 854ef53690bSGarrett Wollman * initialize window to default value, and 855ef53690bSGarrett Wollman * initialize maxseg/maxopd using peer's cached 856ef53690bSGarrett Wollman * MSS. 857ef53690bSGarrett Wollman */ 858603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 859fb59c426SYoshinobu Inoue #ifdef INET6 860fb59c426SYoshinobu Inoue if (isipv6) 861b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 862fb59c426SYoshinobu Inoue else 863fb59c426SYoshinobu Inoue #endif /* INET6 */ 864b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 865ef53690bSGarrett Wollman if (error) 866ef53690bSGarrett Wollman goto out; 867ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 868ef53690bSGarrett Wollman tcp_mss(tp, -1); 869603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 870623dce13SRobert Watson headlocked = 0; 871623dce13SRobert Watson } else if (nam) { 872603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 873623dce13SRobert Watson headlocked = 0; 874623dce13SRobert Watson } 8752c37256eSGarrett Wollman tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 8762cdbfa66SPaul Saab tp->t_flags |= TF_FORCEDATA; 877bc65987aSKip Macy error = tcp_output_send(tp); 8782cdbfa66SPaul Saab tp->t_flags &= ~TF_FORCEDATA; 8792c37256eSGarrett Wollman } 880d1401c90SRobert Watson out: 881d1401c90SRobert Watson TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 8822c37256eSGarrett Wollman ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 8838501a69cSRobert Watson INP_WUNLOCK(inp); 884623dce13SRobert Watson if (headlocked) 885603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 88673fddedaSPeter Grehan return (error); 8872c37256eSGarrett Wollman } 8882c37256eSGarrett Wollman 8892c37256eSGarrett Wollman /* 890a152f8a3SRobert Watson * Abort the TCP. Drop the connection abruptly. 8912c37256eSGarrett Wollman */ 892ac45e92fSRobert Watson static void 8932c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 8942c37256eSGarrett Wollman { 895f76fcf6dSJeffrey Hsu struct inpcb *inp; 896a152f8a3SRobert Watson struct tcpcb *tp = NULL; 897623dce13SRobert Watson TCPDEBUG0; 898c78cbc7bSRobert Watson 899ac45e92fSRobert Watson inp = sotoinpcb(so); 900c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 901c78cbc7bSRobert Watson 902603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 9038501a69cSRobert Watson INP_WLOCK(inp); 904c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 905c78cbc7bSRobert Watson ("tcp_usr_abort: inp_socket == NULL")); 906c78cbc7bSRobert Watson 907c78cbc7bSRobert Watson /* 908a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, drop. 909c78cbc7bSRobert Watson */ 910ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 911ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 912c78cbc7bSRobert Watson tp = intotcpcb(inp); 913a152f8a3SRobert Watson TCPDEBUG1(); 914c78cbc7bSRobert Watson tcp_drop(tp, ECONNABORTED); 915a152f8a3SRobert Watson TCPDEBUG2(PRU_ABORT); 916c78cbc7bSRobert Watson } 917ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 918a152f8a3SRobert Watson SOCK_LOCK(so); 919a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 920a152f8a3SRobert Watson SOCK_UNLOCK(so); 921ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 922a152f8a3SRobert Watson } 9238501a69cSRobert Watson INP_WUNLOCK(inp); 924603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 925a152f8a3SRobert Watson } 926a152f8a3SRobert Watson 927a152f8a3SRobert Watson /* 928a152f8a3SRobert Watson * TCP socket is closed. Start friendly disconnect. 929a152f8a3SRobert Watson */ 930a152f8a3SRobert Watson static void 931a152f8a3SRobert Watson tcp_usr_close(struct socket *so) 932a152f8a3SRobert Watson { 933a152f8a3SRobert Watson struct inpcb *inp; 934a152f8a3SRobert Watson struct tcpcb *tp = NULL; 935a152f8a3SRobert Watson TCPDEBUG0; 936a152f8a3SRobert Watson 937a152f8a3SRobert Watson inp = sotoinpcb(so); 938a152f8a3SRobert Watson KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 939a152f8a3SRobert Watson 940603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 9418501a69cSRobert Watson INP_WLOCK(inp); 942a152f8a3SRobert Watson KASSERT(inp->inp_socket != NULL, 943a152f8a3SRobert Watson ("tcp_usr_close: inp_socket == NULL")); 944a152f8a3SRobert Watson 945a152f8a3SRobert Watson /* 946a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, initiate 947a152f8a3SRobert Watson * a disconnect. 948a152f8a3SRobert Watson */ 949ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 950ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 951a152f8a3SRobert Watson tp = intotcpcb(inp); 952a152f8a3SRobert Watson TCPDEBUG1(); 953a152f8a3SRobert Watson tcp_disconnect(tp); 954a152f8a3SRobert Watson TCPDEBUG2(PRU_CLOSE); 955a152f8a3SRobert Watson } 956ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 957a152f8a3SRobert Watson SOCK_LOCK(so); 958a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 959a152f8a3SRobert Watson SOCK_UNLOCK(so); 960ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 961a152f8a3SRobert Watson } 9628501a69cSRobert Watson INP_WUNLOCK(inp); 963603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 9642c37256eSGarrett Wollman } 9652c37256eSGarrett Wollman 9662c37256eSGarrett Wollman /* 9672c37256eSGarrett Wollman * Receive out-of-band data. 9682c37256eSGarrett Wollman */ 9692c37256eSGarrett Wollman static int 9702c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 9712c37256eSGarrett Wollman { 9722c37256eSGarrett Wollman int error = 0; 973f76fcf6dSJeffrey Hsu struct inpcb *inp; 974623dce13SRobert Watson struct tcpcb *tp = NULL; 9752c37256eSGarrett Wollman 976623dce13SRobert Watson TCPDEBUG0; 977623dce13SRobert Watson inp = sotoinpcb(so); 978623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 9798501a69cSRobert Watson INP_WLOCK(inp); 980ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 98121367f63SSam Leffler error = ECONNRESET; 982623dce13SRobert Watson goto out; 983623dce13SRobert Watson } 984623dce13SRobert Watson tp = intotcpcb(inp); 985623dce13SRobert Watson TCPDEBUG1(); 9862c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 987c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 9884cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 9894cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 9902c37256eSGarrett Wollman error = EINVAL; 9912c37256eSGarrett Wollman goto out; 9922c37256eSGarrett Wollman } 9932c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 9942c37256eSGarrett Wollman error = EWOULDBLOCK; 9952c37256eSGarrett Wollman goto out; 9962c37256eSGarrett Wollman } 9972c37256eSGarrett Wollman m->m_len = 1; 9982c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 9992c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 10002c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1001623dce13SRobert Watson 1002623dce13SRobert Watson out: 1003623dce13SRobert Watson TCPDEBUG2(PRU_RCVOOB); 10048501a69cSRobert Watson INP_WUNLOCK(inp); 1005623dce13SRobert Watson return (error); 10062c37256eSGarrett Wollman } 10072c37256eSGarrett Wollman 10082c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = { 1009756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1010756d52a1SPoul-Henning Kamp .pru_accept = tcp_usr_accept, 1011756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1012756d52a1SPoul-Henning Kamp .pru_bind = tcp_usr_bind, 1013756d52a1SPoul-Henning Kamp .pru_connect = tcp_usr_connect, 1014756d52a1SPoul-Henning Kamp .pru_control = in_control, 1015756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1016756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1017756d52a1SPoul-Henning Kamp .pru_listen = tcp_usr_listen, 101854d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1019756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1020756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1021756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 1022756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 102354d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1024a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1025a152f8a3SRobert Watson .pru_close = tcp_usr_close, 10262c37256eSGarrett Wollman }; 1027df8bae1dSRodney W. Grimes 1028fb59c426SYoshinobu Inoue #ifdef INET6 1029fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = { 1030756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1031756d52a1SPoul-Henning Kamp .pru_accept = tcp6_usr_accept, 1032756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1033756d52a1SPoul-Henning Kamp .pru_bind = tcp6_usr_bind, 1034756d52a1SPoul-Henning Kamp .pru_connect = tcp6_usr_connect, 1035756d52a1SPoul-Henning Kamp .pru_control = in6_control, 1036756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1037756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1038756d52a1SPoul-Henning Kamp .pru_listen = tcp6_usr_listen, 1039756d52a1SPoul-Henning Kamp .pru_peeraddr = in6_mapped_peeraddr, 1040756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1041756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1042756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 1043756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 1044756d52a1SPoul-Henning Kamp .pru_sockaddr = in6_mapped_sockaddr, 1045a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1046a152f8a3SRobert Watson .pru_close = tcp_usr_close, 1047fb59c426SYoshinobu Inoue }; 1048fb59c426SYoshinobu Inoue #endif /* INET6 */ 1049fb59c426SYoshinobu Inoue 1050a0292f23SGarrett Wollman /* 1051a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 1052a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 10535200e00eSIan Dowse * port number if needed. Call in_pcbconnect_setup to do the routing and 10545200e00eSIan Dowse * to choose a local host address (interface). If there is an existing 10555200e00eSIan Dowse * incarnation of the same connection in TIME-WAIT state and if the remote 10565200e00eSIan Dowse * host was sending CC options and if the connection duration was < MSL, then 1057a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 1058a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 1059a0292f23SGarrett Wollman */ 10600312fbe9SPoul-Henning Kamp static int 1061ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1062a0292f23SGarrett Wollman { 1063a0292f23SGarrett Wollman struct inpcb *inp = tp->t_inpcb, *oinp; 1064a0292f23SGarrett Wollman struct socket *so = inp->inp_socket; 10655200e00eSIan Dowse struct in_addr laddr; 10665200e00eSIan Dowse u_short lport; 1067c3229e05SDavid Greenman int error; 1068a0292f23SGarrett Wollman 1069603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 10708501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1071623dce13SRobert Watson 1072a0292f23SGarrett Wollman if (inp->inp_lport == 0) { 1073b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1074a0292f23SGarrett Wollman if (error) 1075a0292f23SGarrett Wollman return error; 1076a0292f23SGarrett Wollman } 1077a0292f23SGarrett Wollman 1078a0292f23SGarrett Wollman /* 1079a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 1080a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 1081a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 1082a0292f23SGarrett Wollman */ 10835200e00eSIan Dowse laddr = inp->inp_laddr; 10845200e00eSIan Dowse lport = inp->inp_lport; 10855200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1086b0330ed9SPawel Jakub Dawidek &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 10875200e00eSIan Dowse if (error && oinp == NULL) 1088d3628763SRodney W. Grimes return error; 1089c94c54e4SAndre Oppermann if (oinp) 1090a0292f23SGarrett Wollman return EADDRINUSE; 10915200e00eSIan Dowse inp->inp_laddr = laddr; 109215bd2b43SDavid Greenman in_pcbrehash(inp); 1093a0292f23SGarrett Wollman 1094087b55eaSAndre Oppermann /* 1095087b55eaSAndre Oppermann * Compute window scaling to request: 1096087b55eaSAndre Oppermann * Scale to fit into sweet spot. See tcp_syncache.c. 1097087b55eaSAndre Oppermann * XXX: This should move to tcp_output(). 1098087b55eaSAndre Oppermann */ 1099a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 11009b3bc6bfSMike Silbersack (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1101a0292f23SGarrett Wollman tp->request_r_scale++; 1102a0292f23SGarrett Wollman 1103a0292f23SGarrett Wollman soisconnecting(so); 110478b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 1105a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_SENT; 1106b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepinit); 1107b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 1108a0292f23SGarrett Wollman tcp_sendseqinit(tp); 1109a45d2726SAndras Olah 1110a0292f23SGarrett Wollman return 0; 1111a0292f23SGarrett Wollman } 1112a0292f23SGarrett Wollman 1113fb59c426SYoshinobu Inoue #ifdef INET6 1114fb59c426SYoshinobu Inoue static int 1115ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1116fb59c426SYoshinobu Inoue { 1117fb59c426SYoshinobu Inoue struct inpcb *inp = tp->t_inpcb, *oinp; 1118fb59c426SYoshinobu Inoue struct socket *so = inp->inp_socket; 1119fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 112088d166bfSBjoern A. Zeeb struct in6_addr addr6; 1121fb59c426SYoshinobu Inoue int error; 1122fb59c426SYoshinobu Inoue 1123603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 11248501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1125623dce13SRobert Watson 1126fb59c426SYoshinobu Inoue if (inp->inp_lport == 0) { 1127b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1128fb59c426SYoshinobu Inoue if (error) 1129fb59c426SYoshinobu Inoue return error; 1130fb59c426SYoshinobu Inoue } 1131fb59c426SYoshinobu Inoue 1132fb59c426SYoshinobu Inoue /* 1133fb59c426SYoshinobu Inoue * Cannot simply call in_pcbconnect, because there might be an 1134fb59c426SYoshinobu Inoue * earlier incarnation of this same connection still in 1135fb59c426SYoshinobu Inoue * TIME_WAIT state, creating an ADDRINUSE error. 1136a1f7e5f8SHajimu UMEMOTO * in6_pcbladdr() also handles scope zone IDs. 1137fb59c426SYoshinobu Inoue */ 1138fb59c426SYoshinobu Inoue error = in6_pcbladdr(inp, nam, &addr6); 1139fb59c426SYoshinobu Inoue if (error) 1140fb59c426SYoshinobu Inoue return error; 1141fb59c426SYoshinobu Inoue oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 1142fb59c426SYoshinobu Inoue &sin6->sin6_addr, sin6->sin6_port, 1143fb59c426SYoshinobu Inoue IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 114488d166bfSBjoern A. Zeeb ? &addr6 1145fb59c426SYoshinobu Inoue : &inp->in6p_laddr, 1146fb59c426SYoshinobu Inoue inp->inp_lport, 0, NULL); 1147c94c54e4SAndre Oppermann if (oinp) 1148fb59c426SYoshinobu Inoue return EADDRINUSE; 1149fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 115088d166bfSBjoern A. Zeeb inp->in6p_laddr = addr6; 1151fb59c426SYoshinobu Inoue inp->in6p_faddr = sin6->sin6_addr; 1152fb59c426SYoshinobu Inoue inp->inp_fport = sin6->sin6_port; 11538a59da30SHajimu UMEMOTO /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 1154fc384fa5SBjoern A. Zeeb inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 1155fc384fa5SBjoern A. Zeeb if (inp->inp_flags & IN6P_AUTOFLOWLABEL) 1156fc384fa5SBjoern A. Zeeb inp->inp_flow |= 11578a59da30SHajimu UMEMOTO (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 1158fb59c426SYoshinobu Inoue in_pcbrehash(inp); 1159fb59c426SYoshinobu Inoue 1160fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1161fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1162970caf60SBjoern A. Zeeb (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1163fb59c426SYoshinobu Inoue tp->request_r_scale++; 1164fb59c426SYoshinobu Inoue 1165fb59c426SYoshinobu Inoue soisconnecting(so); 116678b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 1167fb59c426SYoshinobu Inoue tp->t_state = TCPS_SYN_SENT; 1168b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepinit); 1169b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 1170fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1171fb59c426SYoshinobu Inoue 1172fb59c426SYoshinobu Inoue return 0; 1173fb59c426SYoshinobu Inoue } 1174fb59c426SYoshinobu Inoue #endif /* INET6 */ 1175fb59c426SYoshinobu Inoue 1176cfe8b629SGarrett Wollman /* 1177b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1178b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1179b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1180b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1181b8af5dfaSRobert Watson * from Linux. 1182b8af5dfaSRobert Watson */ 1183b8af5dfaSRobert Watson static void 1184ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1185b8af5dfaSRobert Watson { 1186b8af5dfaSRobert Watson 11878501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1188b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1189b8af5dfaSRobert Watson 1190b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1191b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1192b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 11933529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 1194b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1195b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1196b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1197b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1198b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1199b8af5dfaSRobert Watson } 12001baaf834SBruce M Simpson 120143d94734SJohn Baldwin ti->tcpi_rto = tp->t_rxtcur * tick; 120243d94734SJohn Baldwin ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick; 12031baaf834SBruce M Simpson ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 12041baaf834SBruce M Simpson ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 12051baaf834SBruce M Simpson 1206b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1207b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1208b8af5dfaSRobert Watson 1209b8af5dfaSRobert Watson /* 1210b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1211b8af5dfaSRobert Watson */ 1212c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1213535fbad6SKip Macy ti->tcpi_rcv_nxt = tp->rcv_nxt; 1214b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 1215*1c18314dSAndre Oppermann ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1216535fbad6SKip Macy ti->tcpi_snd_nxt = tp->snd_nxt; 121743d94734SJohn Baldwin ti->tcpi_snd_mss = tp->t_maxseg; 121843d94734SJohn Baldwin ti->tcpi_rcv_mss = tp->t_maxseg; 1219535fbad6SKip Macy if (tp->t_flags & TF_TOE) 1220535fbad6SKip Macy ti->tcpi_options |= TCPI_OPT_TOE; 1221b8af5dfaSRobert Watson } 1222b8af5dfaSRobert Watson 1223b8af5dfaSRobert Watson /* 12241e8f5ffaSRobert Watson * tcp_ctloutput() must drop the inpcb lock before performing copyin on 12251e8f5ffaSRobert Watson * socket option arguments. When it re-acquires the lock after the copy, it 12261e8f5ffaSRobert Watson * has to revalidate that the connection is still valid for the socket 12271e8f5ffaSRobert Watson * option. 1228cfe8b629SGarrett Wollman */ 12298501a69cSRobert Watson #define INP_WLOCK_RECHECK(inp) do { \ 12308501a69cSRobert Watson INP_WLOCK(inp); \ 1231ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 12328501a69cSRobert Watson INP_WUNLOCK(inp); \ 12331e8f5ffaSRobert Watson return (ECONNRESET); \ 12341e8f5ffaSRobert Watson } \ 12351e8f5ffaSRobert Watson tp = intotcpcb(inp); \ 12361e8f5ffaSRobert Watson } while(0) 12371e8f5ffaSRobert Watson 1238df8bae1dSRodney W. Grimes int 1239ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1240df8bae1dSRodney W. Grimes { 12413f9d1ef9SRobert Watson int error, opt, optval; 1242df8bae1dSRodney W. Grimes struct inpcb *inp; 1243cfe8b629SGarrett Wollman struct tcpcb *tp; 1244b8af5dfaSRobert Watson struct tcp_info ti; 1245df8bae1dSRodney W. Grimes 1246cfe8b629SGarrett Wollman error = 0; 1247df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1248623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 12498501a69cSRobert Watson INP_WLOCK(inp); 1250cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 1251fb59c426SYoshinobu Inoue #ifdef INET6 12525cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 12538501a69cSRobert Watson INP_WUNLOCK(inp); 1254fb59c426SYoshinobu Inoue error = ip6_ctloutput(so, sopt); 12551e8f5ffaSRobert Watson } else { 1256fb59c426SYoshinobu Inoue #endif /* INET6 */ 12578501a69cSRobert Watson INP_WUNLOCK(inp); 1258cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 12591e8f5ffaSRobert Watson #ifdef INET6 12601e8f5ffaSRobert Watson } 12611e8f5ffaSRobert Watson #endif 1262df8bae1dSRodney W. Grimes return (error); 1263df8bae1dSRodney W. Grimes } 1264ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 12658501a69cSRobert Watson INP_WUNLOCK(inp); 12661e8f5ffaSRobert Watson return (ECONNRESET); 1267623dce13SRobert Watson } 1268df8bae1dSRodney W. Grimes 1269cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1270cfe8b629SGarrett Wollman case SOPT_SET: 1271cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 12721cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 127388f6b043SBruce M Simpson case TCP_MD5SIG: 12748501a69cSRobert Watson INP_WUNLOCK(inp); 12751cfd4b53SBruce M Simpson error = sooptcopyin(sopt, &optval, sizeof optval, 12761cfd4b53SBruce M Simpson sizeof optval); 12771cfd4b53SBruce M Simpson if (error) 12781e8f5ffaSRobert Watson return (error); 12791cfd4b53SBruce M Simpson 12808501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 12811cfd4b53SBruce M Simpson if (optval > 0) 12821cfd4b53SBruce M Simpson tp->t_flags |= TF_SIGNATURE; 12831cfd4b53SBruce M Simpson else 12841cfd4b53SBruce M Simpson tp->t_flags &= ~TF_SIGNATURE; 12858501a69cSRobert Watson INP_WUNLOCK(inp); 12861cfd4b53SBruce M Simpson break; 12871cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 1288df8bae1dSRodney W. Grimes case TCP_NODELAY: 1289cfe8b629SGarrett Wollman case TCP_NOOPT: 12908501a69cSRobert Watson INP_WUNLOCK(inp); 1291cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1292cfe8b629SGarrett Wollman sizeof optval); 1293cfe8b629SGarrett Wollman if (error) 12941e8f5ffaSRobert Watson return (error); 1295cfe8b629SGarrett Wollman 12968501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1297cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1298cfe8b629SGarrett Wollman case TCP_NODELAY: 1299cfe8b629SGarrett Wollman opt = TF_NODELAY; 1300cfe8b629SGarrett Wollman break; 1301cfe8b629SGarrett Wollman case TCP_NOOPT: 1302cfe8b629SGarrett Wollman opt = TF_NOOPT; 1303cfe8b629SGarrett Wollman break; 1304cfe8b629SGarrett Wollman default: 1305cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 1306cfe8b629SGarrett Wollman break; 1307cfe8b629SGarrett Wollman } 1308cfe8b629SGarrett Wollman 1309cfe8b629SGarrett Wollman if (optval) 1310cfe8b629SGarrett Wollman tp->t_flags |= opt; 1311df8bae1dSRodney W. Grimes else 1312cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 13138501a69cSRobert Watson INP_WUNLOCK(inp); 1314df8bae1dSRodney W. Grimes break; 1315df8bae1dSRodney W. Grimes 1316007581c0SJonathan Lemon case TCP_NOPUSH: 13178501a69cSRobert Watson INP_WUNLOCK(inp); 1318007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 1319007581c0SJonathan Lemon sizeof optval); 1320007581c0SJonathan Lemon if (error) 13211e8f5ffaSRobert Watson return (error); 1322007581c0SJonathan Lemon 13238501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1324007581c0SJonathan Lemon if (optval) 1325007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 1326007581c0SJonathan Lemon else { 1327007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 1328007581c0SJonathan Lemon error = tcp_output(tp); 1329007581c0SJonathan Lemon } 13308501a69cSRobert Watson INP_WUNLOCK(inp); 1331007581c0SJonathan Lemon break; 1332007581c0SJonathan Lemon 1333df8bae1dSRodney W. Grimes case TCP_MAXSEG: 13348501a69cSRobert Watson INP_WUNLOCK(inp); 1335cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1336cfe8b629SGarrett Wollman sizeof optval); 1337cfe8b629SGarrett Wollman if (error) 13381e8f5ffaSRobert Watson return (error); 1339df8bae1dSRodney W. Grimes 13408501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 134153369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 1342603724d3SBjoern A. Zeeb optval + 40 >= V_tcp_minmss) 1343cfe8b629SGarrett Wollman tp->t_maxseg = optval; 1344a0292f23SGarrett Wollman else 1345a0292f23SGarrett Wollman error = EINVAL; 13468501a69cSRobert Watson INP_WUNLOCK(inp); 1347a0292f23SGarrett Wollman break; 1348a0292f23SGarrett Wollman 1349b8af5dfaSRobert Watson case TCP_INFO: 13508501a69cSRobert Watson INP_WUNLOCK(inp); 1351b8af5dfaSRobert Watson error = EINVAL; 1352b8af5dfaSRobert Watson break; 1353b8af5dfaSRobert Watson 1354df8bae1dSRodney W. Grimes default: 13558501a69cSRobert Watson INP_WUNLOCK(inp); 1356df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1357df8bae1dSRodney W. Grimes break; 1358df8bae1dSRodney W. Grimes } 1359df8bae1dSRodney W. Grimes break; 1360df8bae1dSRodney W. Grimes 1361cfe8b629SGarrett Wollman case SOPT_GET: 13621e8f5ffaSRobert Watson tp = intotcpcb(inp); 1363cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 13641cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 136588f6b043SBruce M Simpson case TCP_MD5SIG: 13661cfd4b53SBruce M Simpson optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 13678501a69cSRobert Watson INP_WUNLOCK(inp); 1368b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 13691cfd4b53SBruce M Simpson break; 1370265ed012SBruce M Simpson #endif 13711e8f5ffaSRobert Watson 1372df8bae1dSRodney W. Grimes case TCP_NODELAY: 1373cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 13748501a69cSRobert Watson INP_WUNLOCK(inp); 1375b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1376df8bae1dSRodney W. Grimes break; 1377df8bae1dSRodney W. Grimes case TCP_MAXSEG: 1378cfe8b629SGarrett Wollman optval = tp->t_maxseg; 13798501a69cSRobert Watson INP_WUNLOCK(inp); 1380b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1381df8bae1dSRodney W. Grimes break; 1382a0292f23SGarrett Wollman case TCP_NOOPT: 1383cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 13848501a69cSRobert Watson INP_WUNLOCK(inp); 1385b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1386a0292f23SGarrett Wollman break; 1387a0292f23SGarrett Wollman case TCP_NOPUSH: 1388cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 13898501a69cSRobert Watson INP_WUNLOCK(inp); 1390b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1391b8af5dfaSRobert Watson break; 1392b8af5dfaSRobert Watson case TCP_INFO: 1393b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 13948501a69cSRobert Watson INP_WUNLOCK(inp); 1395b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 1396a0292f23SGarrett Wollman break; 1397df8bae1dSRodney W. Grimes default: 13988501a69cSRobert Watson INP_WUNLOCK(inp); 1399df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1400df8bae1dSRodney W. Grimes break; 1401df8bae1dSRodney W. Grimes } 1402df8bae1dSRodney W. Grimes break; 1403df8bae1dSRodney W. Grimes } 1404df8bae1dSRodney W. Grimes return (error); 1405df8bae1dSRodney W. Grimes } 14068501a69cSRobert Watson #undef INP_WLOCK_RECHECK 1407df8bae1dSRodney W. Grimes 140826e30fbbSDavid Greenman /* 140926e30fbbSDavid Greenman * tcp_sendspace and tcp_recvspace are the default send and receive window 141026e30fbbSDavid Greenman * sizes, respectively. These are obsolescent (this information should 141126e30fbbSDavid Greenman * be set by the route). 141226e30fbbSDavid Greenman */ 141381e561cdSDavid E. O'Brien u_long tcp_sendspace = 1024*32; 1414e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 14153d177f46SBill Fumerola &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 141681e561cdSDavid E. O'Brien u_long tcp_recvspace = 1024*64; 1417e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 14183d177f46SBill Fumerola &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1419df8bae1dSRodney W. Grimes 1420df8bae1dSRodney W. Grimes /* 1421df8bae1dSRodney W. Grimes * Attach TCP protocol to socket, allocating 1422df8bae1dSRodney W. Grimes * internet protocol control block, tcp control block, 1423df8bae1dSRodney W. Grimes * bufer space, and entering LISTEN state if to accept connections. 1424df8bae1dSRodney W. Grimes */ 14250312fbe9SPoul-Henning Kamp static int 1426ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so) 1427df8bae1dSRodney W. Grimes { 1428ad3f9ab3SAndre Oppermann struct tcpcb *tp; 1429df8bae1dSRodney W. Grimes struct inpcb *inp; 1430df8bae1dSRodney W. Grimes int error; 1431df8bae1dSRodney W. Grimes 1432df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1433df8bae1dSRodney W. Grimes error = soreserve(so, tcp_sendspace, tcp_recvspace); 1434df8bae1dSRodney W. Grimes if (error) 1435df8bae1dSRodney W. Grimes return (error); 1436df8bae1dSRodney W. Grimes } 14376741ecf5SAndre Oppermann so->so_rcv.sb_flags |= SB_AUTOSIZE; 14386741ecf5SAndre Oppermann so->so_snd.sb_flags |= SB_AUTOSIZE; 1439603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 1440603724d3SBjoern A. Zeeb error = in_pcballoc(so, &V_tcbinfo); 1441f2de87feSRobert Watson if (error) { 1442603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 1443df8bae1dSRodney W. Grimes return (error); 1444f2de87feSRobert Watson } 1445df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1446fb59c426SYoshinobu Inoue #ifdef INET6 14475cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 1448fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 1449fb59c426SYoshinobu Inoue inp->in6p_hops = -1; /* use kernel default */ 1450fb59c426SYoshinobu Inoue } 1451fb59c426SYoshinobu Inoue else 1452fb59c426SYoshinobu Inoue #endif 1453cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1454df8bae1dSRodney W. Grimes tp = tcp_newtcpcb(inp); 1455623dce13SRobert Watson if (tp == NULL) { 1456df8bae1dSRodney W. Grimes in_pcbdetach(inp); 14570206cdb8SBjoern A. Zeeb in_pcbfree(inp); 1458603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 1459df8bae1dSRodney W. Grimes return (ENOBUFS); 1460df8bae1dSRodney W. Grimes } 1461df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 14628501a69cSRobert Watson INP_WUNLOCK(inp); 1463603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 1464df8bae1dSRodney W. Grimes return (0); 1465df8bae1dSRodney W. Grimes } 1466df8bae1dSRodney W. Grimes 1467df8bae1dSRodney W. Grimes /* 1468df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 1469df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 1470df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 1471df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 1472df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 1473df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 1474df8bae1dSRodney W. Grimes */ 1475623dce13SRobert Watson static void 1476ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp) 1477df8bae1dSRodney W. Grimes { 1478e6e0b5ffSRobert Watson struct inpcb *inp = tp->t_inpcb; 1479e6e0b5ffSRobert Watson struct socket *so = inp->inp_socket; 1480e6e0b5ffSRobert Watson 1481603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 14828501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1483df8bae1dSRodney W. Grimes 1484623dce13SRobert Watson /* 1485623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 1486623dce13SRobert Watson * socket is still open. 1487623dce13SRobert Watson */ 1488623dce13SRobert Watson if (tp->t_state < TCPS_ESTABLISHED) { 1489df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1490623dce13SRobert Watson KASSERT(tp != NULL, 1491623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 1492623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 1493243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 1494623dce13SRobert Watson KASSERT(tp != NULL, 1495623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 1496623dce13SRobert Watson } else { 1497df8bae1dSRodney W. Grimes soisdisconnecting(so); 1498df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 1499623dce13SRobert Watson tcp_usrclosed(tp); 1500ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 1501bc65987aSKip Macy tcp_output_disconnect(tp); 1502df8bae1dSRodney W. Grimes } 1503df8bae1dSRodney W. Grimes } 1504df8bae1dSRodney W. Grimes 1505df8bae1dSRodney W. Grimes /* 1506df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 1507df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 1508df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1509df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 1510df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 1511df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 1512df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 1513df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 1514df8bae1dSRodney W. Grimes */ 1515623dce13SRobert Watson static void 1516ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp) 1517df8bae1dSRodney W. Grimes { 1518df8bae1dSRodney W. Grimes 1519603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 15208501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1521e6e0b5ffSRobert Watson 1522df8bae1dSRodney W. Grimes switch (tp->t_state) { 1523df8bae1dSRodney W. Grimes case TCPS_LISTEN: 1524bc65987aSKip Macy tcp_offload_listen_close(tp); 1525bc65987aSKip Macy /* FALLTHROUGH */ 1526bc65987aSKip Macy case TCPS_CLOSED: 1527df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 1528df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1529623dce13SRobert Watson /* 1530623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 1531623dce13SRobert Watson * still open. 1532623dce13SRobert Watson */ 1533623dce13SRobert Watson KASSERT(tp != NULL, 1534623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 1535df8bae1dSRodney W. Grimes break; 1536df8bae1dSRodney W. Grimes 1537a0292f23SGarrett Wollman case TCPS_SYN_SENT: 1538df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1539a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 1540a0292f23SGarrett Wollman break; 1541a0292f23SGarrett Wollman 1542df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1543df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_1; 1544df8bae1dSRodney W. Grimes break; 1545df8bae1dSRodney W. Grimes 1546df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1547df8bae1dSRodney W. Grimes tp->t_state = TCPS_LAST_ACK; 1548df8bae1dSRodney W. Grimes break; 1549df8bae1dSRodney W. Grimes } 1550abc7d910SRobert Watson if (tp->t_state >= TCPS_FIN_WAIT_2) { 1551df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 1552abc7d910SRobert Watson /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 15537c72af87SMohan Srinivasan if (tp->t_state == TCPS_FIN_WAIT_2) { 15547c72af87SMohan Srinivasan int timeout; 15557c72af87SMohan Srinivasan 15567c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 15577c72af87SMohan Srinivasan tcp_finwait2_timeout : tcp_maxidle; 1558b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 1559b6239c4aSAndras Olah } 1560df8bae1dSRodney W. Grimes } 15617c72af87SMohan Srinivasan } 1562497057eeSRobert Watson 1563497057eeSRobert Watson #ifdef DDB 1564497057eeSRobert Watson static void 1565497057eeSRobert Watson db_print_indent(int indent) 1566497057eeSRobert Watson { 1567497057eeSRobert Watson int i; 1568497057eeSRobert Watson 1569497057eeSRobert Watson for (i = 0; i < indent; i++) 1570497057eeSRobert Watson db_printf(" "); 1571497057eeSRobert Watson } 1572497057eeSRobert Watson 1573497057eeSRobert Watson static void 1574497057eeSRobert Watson db_print_tstate(int t_state) 1575497057eeSRobert Watson { 1576497057eeSRobert Watson 1577497057eeSRobert Watson switch (t_state) { 1578497057eeSRobert Watson case TCPS_CLOSED: 1579497057eeSRobert Watson db_printf("TCPS_CLOSED"); 1580497057eeSRobert Watson return; 1581497057eeSRobert Watson 1582497057eeSRobert Watson case TCPS_LISTEN: 1583497057eeSRobert Watson db_printf("TCPS_LISTEN"); 1584497057eeSRobert Watson return; 1585497057eeSRobert Watson 1586497057eeSRobert Watson case TCPS_SYN_SENT: 1587497057eeSRobert Watson db_printf("TCPS_SYN_SENT"); 1588497057eeSRobert Watson return; 1589497057eeSRobert Watson 1590497057eeSRobert Watson case TCPS_SYN_RECEIVED: 1591497057eeSRobert Watson db_printf("TCPS_SYN_RECEIVED"); 1592497057eeSRobert Watson return; 1593497057eeSRobert Watson 1594497057eeSRobert Watson case TCPS_ESTABLISHED: 1595497057eeSRobert Watson db_printf("TCPS_ESTABLISHED"); 1596497057eeSRobert Watson return; 1597497057eeSRobert Watson 1598497057eeSRobert Watson case TCPS_CLOSE_WAIT: 1599497057eeSRobert Watson db_printf("TCPS_CLOSE_WAIT"); 1600497057eeSRobert Watson return; 1601497057eeSRobert Watson 1602497057eeSRobert Watson case TCPS_FIN_WAIT_1: 1603497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_1"); 1604497057eeSRobert Watson return; 1605497057eeSRobert Watson 1606497057eeSRobert Watson case TCPS_CLOSING: 1607497057eeSRobert Watson db_printf("TCPS_CLOSING"); 1608497057eeSRobert Watson return; 1609497057eeSRobert Watson 1610497057eeSRobert Watson case TCPS_LAST_ACK: 1611497057eeSRobert Watson db_printf("TCPS_LAST_ACK"); 1612497057eeSRobert Watson return; 1613497057eeSRobert Watson 1614497057eeSRobert Watson case TCPS_FIN_WAIT_2: 1615497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_2"); 1616497057eeSRobert Watson return; 1617497057eeSRobert Watson 1618497057eeSRobert Watson case TCPS_TIME_WAIT: 1619497057eeSRobert Watson db_printf("TCPS_TIME_WAIT"); 1620497057eeSRobert Watson return; 1621497057eeSRobert Watson 1622497057eeSRobert Watson default: 1623497057eeSRobert Watson db_printf("unknown"); 1624497057eeSRobert Watson return; 1625497057eeSRobert Watson } 1626497057eeSRobert Watson } 1627497057eeSRobert Watson 1628497057eeSRobert Watson static void 1629497057eeSRobert Watson db_print_tflags(u_int t_flags) 1630497057eeSRobert Watson { 1631497057eeSRobert Watson int comma; 1632497057eeSRobert Watson 1633497057eeSRobert Watson comma = 0; 1634497057eeSRobert Watson if (t_flags & TF_ACKNOW) { 1635497057eeSRobert Watson db_printf("%sTF_ACKNOW", comma ? ", " : ""); 1636497057eeSRobert Watson comma = 1; 1637497057eeSRobert Watson } 1638497057eeSRobert Watson if (t_flags & TF_DELACK) { 1639497057eeSRobert Watson db_printf("%sTF_DELACK", comma ? ", " : ""); 1640497057eeSRobert Watson comma = 1; 1641497057eeSRobert Watson } 1642497057eeSRobert Watson if (t_flags & TF_NODELAY) { 1643497057eeSRobert Watson db_printf("%sTF_NODELAY", comma ? ", " : ""); 1644497057eeSRobert Watson comma = 1; 1645497057eeSRobert Watson } 1646497057eeSRobert Watson if (t_flags & TF_NOOPT) { 1647497057eeSRobert Watson db_printf("%sTF_NOOPT", comma ? ", " : ""); 1648497057eeSRobert Watson comma = 1; 1649497057eeSRobert Watson } 1650497057eeSRobert Watson if (t_flags & TF_SENTFIN) { 1651497057eeSRobert Watson db_printf("%sTF_SENTFIN", comma ? ", " : ""); 1652497057eeSRobert Watson comma = 1; 1653497057eeSRobert Watson } 1654497057eeSRobert Watson if (t_flags & TF_REQ_SCALE) { 1655497057eeSRobert Watson db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 1656497057eeSRobert Watson comma = 1; 1657497057eeSRobert Watson } 1658497057eeSRobert Watson if (t_flags & TF_RCVD_SCALE) { 1659497057eeSRobert Watson db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 1660497057eeSRobert Watson comma = 1; 1661497057eeSRobert Watson } 1662497057eeSRobert Watson if (t_flags & TF_REQ_TSTMP) { 1663497057eeSRobert Watson db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 1664497057eeSRobert Watson comma = 1; 1665497057eeSRobert Watson } 1666497057eeSRobert Watson if (t_flags & TF_RCVD_TSTMP) { 1667497057eeSRobert Watson db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 1668497057eeSRobert Watson comma = 1; 1669497057eeSRobert Watson } 1670497057eeSRobert Watson if (t_flags & TF_SACK_PERMIT) { 1671497057eeSRobert Watson db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 1672497057eeSRobert Watson comma = 1; 1673497057eeSRobert Watson } 1674497057eeSRobert Watson if (t_flags & TF_NEEDSYN) { 1675497057eeSRobert Watson db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 1676497057eeSRobert Watson comma = 1; 1677497057eeSRobert Watson } 1678497057eeSRobert Watson if (t_flags & TF_NEEDFIN) { 1679497057eeSRobert Watson db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 1680497057eeSRobert Watson comma = 1; 1681497057eeSRobert Watson } 1682497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 1683497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 1684497057eeSRobert Watson comma = 1; 1685497057eeSRobert Watson } 1686497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 1687497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 1688497057eeSRobert Watson comma = 1; 1689497057eeSRobert Watson } 1690497057eeSRobert Watson if (t_flags & TF_MORETOCOME) { 1691497057eeSRobert Watson db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 1692497057eeSRobert Watson comma = 1; 1693497057eeSRobert Watson } 1694497057eeSRobert Watson if (t_flags & TF_LQ_OVERFLOW) { 1695497057eeSRobert Watson db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 1696497057eeSRobert Watson comma = 1; 1697497057eeSRobert Watson } 1698497057eeSRobert Watson if (t_flags & TF_LASTIDLE) { 1699497057eeSRobert Watson db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 1700497057eeSRobert Watson comma = 1; 1701497057eeSRobert Watson } 1702497057eeSRobert Watson if (t_flags & TF_RXWIN0SENT) { 1703497057eeSRobert Watson db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 1704497057eeSRobert Watson comma = 1; 1705497057eeSRobert Watson } 1706497057eeSRobert Watson if (t_flags & TF_FASTRECOVERY) { 1707497057eeSRobert Watson db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 1708497057eeSRobert Watson comma = 1; 1709497057eeSRobert Watson } 1710497057eeSRobert Watson if (t_flags & TF_WASFRECOVERY) { 1711497057eeSRobert Watson db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 1712497057eeSRobert Watson comma = 1; 1713497057eeSRobert Watson } 1714497057eeSRobert Watson if (t_flags & TF_SIGNATURE) { 1715497057eeSRobert Watson db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 1716497057eeSRobert Watson comma = 1; 1717497057eeSRobert Watson } 1718497057eeSRobert Watson if (t_flags & TF_FORCEDATA) { 1719497057eeSRobert Watson db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 1720497057eeSRobert Watson comma = 1; 1721497057eeSRobert Watson } 1722497057eeSRobert Watson if (t_flags & TF_TSO) { 1723497057eeSRobert Watson db_printf("%sTF_TSO", comma ? ", " : ""); 1724497057eeSRobert Watson comma = 1; 1725497057eeSRobert Watson } 1726f2512ba1SRui Paulo if (t_flags & TF_ECN_PERMIT) { 1727f2512ba1SRui Paulo db_printf("%sTF_ECN_PERMIT", comma ? ", " : ""); 1728f2512ba1SRui Paulo comma = 1; 1729f2512ba1SRui Paulo } 1730497057eeSRobert Watson } 1731497057eeSRobert Watson 1732497057eeSRobert Watson static void 1733497057eeSRobert Watson db_print_toobflags(char t_oobflags) 1734497057eeSRobert Watson { 1735497057eeSRobert Watson int comma; 1736497057eeSRobert Watson 1737497057eeSRobert Watson comma = 0; 1738497057eeSRobert Watson if (t_oobflags & TCPOOB_HAVEDATA) { 1739497057eeSRobert Watson db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 1740497057eeSRobert Watson comma = 1; 1741497057eeSRobert Watson } 1742497057eeSRobert Watson if (t_oobflags & TCPOOB_HADDATA) { 1743497057eeSRobert Watson db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 1744497057eeSRobert Watson comma = 1; 1745497057eeSRobert Watson } 1746497057eeSRobert Watson } 1747497057eeSRobert Watson 1748497057eeSRobert Watson static void 1749497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 1750497057eeSRobert Watson { 1751497057eeSRobert Watson 1752497057eeSRobert Watson db_print_indent(indent); 1753497057eeSRobert Watson db_printf("%s at %p\n", name, tp); 1754497057eeSRobert Watson 1755497057eeSRobert Watson indent += 2; 1756497057eeSRobert Watson 1757497057eeSRobert Watson db_print_indent(indent); 1758497057eeSRobert Watson db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 1759497057eeSRobert Watson LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 1760497057eeSRobert Watson 1761497057eeSRobert Watson db_print_indent(indent); 176285d94372SRobert Watson db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 1763e2f2059fSMike Silbersack &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 1764497057eeSRobert Watson 1765497057eeSRobert Watson db_print_indent(indent); 1766e2f2059fSMike Silbersack db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 1767e2f2059fSMike Silbersack &tp->t_timers->tt_delack, tp->t_inpcb); 1768497057eeSRobert Watson 1769497057eeSRobert Watson db_print_indent(indent); 1770497057eeSRobert Watson db_printf("t_state: %d (", tp->t_state); 1771497057eeSRobert Watson db_print_tstate(tp->t_state); 1772497057eeSRobert Watson db_printf(")\n"); 1773497057eeSRobert Watson 1774497057eeSRobert Watson db_print_indent(indent); 1775497057eeSRobert Watson db_printf("t_flags: 0x%x (", tp->t_flags); 1776497057eeSRobert Watson db_print_tflags(tp->t_flags); 1777497057eeSRobert Watson db_printf(")\n"); 1778497057eeSRobert Watson 1779497057eeSRobert Watson db_print_indent(indent); 1780497057eeSRobert Watson db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 1781497057eeSRobert Watson tp->snd_una, tp->snd_max, tp->snd_nxt); 1782497057eeSRobert Watson 1783497057eeSRobert Watson db_print_indent(indent); 1784497057eeSRobert Watson db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 1785497057eeSRobert Watson tp->snd_up, tp->snd_wl1, tp->snd_wl2); 1786497057eeSRobert Watson 1787497057eeSRobert Watson db_print_indent(indent); 1788497057eeSRobert Watson db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 1789497057eeSRobert Watson tp->iss, tp->irs, tp->rcv_nxt); 1790497057eeSRobert Watson 1791497057eeSRobert Watson db_print_indent(indent); 1792497057eeSRobert Watson db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n", 1793497057eeSRobert Watson tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 1794497057eeSRobert Watson 1795497057eeSRobert Watson db_print_indent(indent); 1796*1c18314dSAndre Oppermann db_printf("snd_wnd: %lu snd_cwnd: %lu\n", 1797*1c18314dSAndre Oppermann tp->snd_wnd, tp->snd_cwnd); 1798497057eeSRobert Watson 1799497057eeSRobert Watson db_print_indent(indent); 1800*1c18314dSAndre Oppermann db_printf("snd_ssthresh: %lu snd_recover: " 1801*1c18314dSAndre Oppermann "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 1802497057eeSRobert Watson 1803497057eeSRobert Watson db_print_indent(indent); 18049f78a87aSJohn Baldwin db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n", 1805497057eeSRobert Watson tp->t_maxopd, tp->t_rcvtime, tp->t_starttime); 1806497057eeSRobert Watson 1807497057eeSRobert Watson db_print_indent(indent); 1808*1c18314dSAndre Oppermann db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 1809*1c18314dSAndre Oppermann tp->t_rtttime, tp->t_rtseq); 1810497057eeSRobert Watson 1811497057eeSRobert Watson db_print_indent(indent); 1812*1c18314dSAndre Oppermann db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 1813*1c18314dSAndre Oppermann tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 1814497057eeSRobert Watson 1815497057eeSRobert Watson db_print_indent(indent); 1816497057eeSRobert Watson db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 1817497057eeSRobert Watson "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 1818497057eeSRobert Watson tp->t_rttbest); 1819497057eeSRobert Watson 1820497057eeSRobert Watson db_print_indent(indent); 1821497057eeSRobert Watson db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n", 1822497057eeSRobert Watson tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 1823497057eeSRobert Watson 1824497057eeSRobert Watson db_print_indent(indent); 1825497057eeSRobert Watson db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 1826497057eeSRobert Watson db_print_toobflags(tp->t_oobflags); 1827497057eeSRobert Watson db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 1828497057eeSRobert Watson 1829497057eeSRobert Watson db_print_indent(indent); 1830497057eeSRobert Watson db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 1831497057eeSRobert Watson tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 1832497057eeSRobert Watson 1833497057eeSRobert Watson db_print_indent(indent); 18349f78a87aSJohn Baldwin db_printf("ts_recent: %u ts_recent_age: %u\n", 18351a553740SAndre Oppermann tp->ts_recent, tp->ts_recent_age); 1836497057eeSRobert Watson 1837497057eeSRobert Watson db_print_indent(indent); 1838497057eeSRobert Watson db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 1839497057eeSRobert Watson "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 1840497057eeSRobert Watson 1841497057eeSRobert Watson db_print_indent(indent); 1842497057eeSRobert Watson db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x " 18439f78a87aSJohn Baldwin "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 1844497057eeSRobert Watson tp->snd_recover_prev, tp->t_badrxtwin); 1845497057eeSRobert Watson 1846497057eeSRobert Watson db_print_indent(indent); 18473529149eSAndre Oppermann db_printf("snd_numholes: %d snd_holes first: %p\n", 18483529149eSAndre Oppermann tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 1849497057eeSRobert Watson 1850497057eeSRobert Watson db_print_indent(indent); 1851497057eeSRobert Watson db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: " 1852497057eeSRobert Watson "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata); 1853497057eeSRobert Watson 1854497057eeSRobert Watson /* Skip sackblks, sackhint. */ 1855497057eeSRobert Watson 1856497057eeSRobert Watson db_print_indent(indent); 1857497057eeSRobert Watson db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 1858497057eeSRobert Watson tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 1859497057eeSRobert Watson } 1860497057eeSRobert Watson 1861497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 1862497057eeSRobert Watson { 1863497057eeSRobert Watson struct tcpcb *tp; 1864497057eeSRobert Watson 1865497057eeSRobert Watson if (!have_addr) { 1866497057eeSRobert Watson db_printf("usage: show tcpcb <addr>\n"); 1867497057eeSRobert Watson return; 1868497057eeSRobert Watson } 1869497057eeSRobert Watson tp = (struct tcpcb *)addr; 1870497057eeSRobert Watson 1871497057eeSRobert Watson db_print_tcpcb(tp, "tcpcb", 0); 1872497057eeSRobert Watson } 1873497057eeSRobert Watson #endif 1874