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); 11081fcc99b5SMatthew Dillon tp->t_bw_rtseq = tp->iss; 1109a0292f23SGarrett Wollman tcp_sendseqinit(tp); 1110a45d2726SAndras Olah 1111a0292f23SGarrett Wollman return 0; 1112a0292f23SGarrett Wollman } 1113a0292f23SGarrett Wollman 1114fb59c426SYoshinobu Inoue #ifdef INET6 1115fb59c426SYoshinobu Inoue static int 1116ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1117fb59c426SYoshinobu Inoue { 1118fb59c426SYoshinobu Inoue struct inpcb *inp = tp->t_inpcb, *oinp; 1119fb59c426SYoshinobu Inoue struct socket *so = inp->inp_socket; 1120fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 112188d166bfSBjoern A. Zeeb struct in6_addr addr6; 1122fb59c426SYoshinobu Inoue int error; 1123fb59c426SYoshinobu Inoue 1124603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 11258501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1126623dce13SRobert Watson 1127fb59c426SYoshinobu Inoue if (inp->inp_lport == 0) { 1128b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1129fb59c426SYoshinobu Inoue if (error) 1130fb59c426SYoshinobu Inoue return error; 1131fb59c426SYoshinobu Inoue } 1132fb59c426SYoshinobu Inoue 1133fb59c426SYoshinobu Inoue /* 1134fb59c426SYoshinobu Inoue * Cannot simply call in_pcbconnect, because there might be an 1135fb59c426SYoshinobu Inoue * earlier incarnation of this same connection still in 1136fb59c426SYoshinobu Inoue * TIME_WAIT state, creating an ADDRINUSE error. 1137a1f7e5f8SHajimu UMEMOTO * in6_pcbladdr() also handles scope zone IDs. 1138fb59c426SYoshinobu Inoue */ 1139fb59c426SYoshinobu Inoue error = in6_pcbladdr(inp, nam, &addr6); 1140fb59c426SYoshinobu Inoue if (error) 1141fb59c426SYoshinobu Inoue return error; 1142fb59c426SYoshinobu Inoue oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 1143fb59c426SYoshinobu Inoue &sin6->sin6_addr, sin6->sin6_port, 1144fb59c426SYoshinobu Inoue IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 114588d166bfSBjoern A. Zeeb ? &addr6 1146fb59c426SYoshinobu Inoue : &inp->in6p_laddr, 1147fb59c426SYoshinobu Inoue inp->inp_lport, 0, NULL); 1148c94c54e4SAndre Oppermann if (oinp) 1149fb59c426SYoshinobu Inoue return EADDRINUSE; 1150fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 115188d166bfSBjoern A. Zeeb inp->in6p_laddr = addr6; 1152fb59c426SYoshinobu Inoue inp->in6p_faddr = sin6->sin6_addr; 1153fb59c426SYoshinobu Inoue inp->inp_fport = sin6->sin6_port; 11548a59da30SHajimu UMEMOTO /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 1155fc384fa5SBjoern A. Zeeb inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 1156fc384fa5SBjoern A. Zeeb if (inp->inp_flags & IN6P_AUTOFLOWLABEL) 1157fc384fa5SBjoern A. Zeeb inp->inp_flow |= 11588a59da30SHajimu UMEMOTO (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 1159fb59c426SYoshinobu Inoue in_pcbrehash(inp); 1160fb59c426SYoshinobu Inoue 1161fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1162fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1163970caf60SBjoern A. Zeeb (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1164fb59c426SYoshinobu Inoue tp->request_r_scale++; 1165fb59c426SYoshinobu Inoue 1166fb59c426SYoshinobu Inoue soisconnecting(so); 116778b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 1168fb59c426SYoshinobu Inoue tp->t_state = TCPS_SYN_SENT; 1169b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepinit); 1170b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 11711fcc99b5SMatthew Dillon tp->t_bw_rtseq = tp->iss; 1172fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1173fb59c426SYoshinobu Inoue 1174fb59c426SYoshinobu Inoue return 0; 1175fb59c426SYoshinobu Inoue } 1176fb59c426SYoshinobu Inoue #endif /* INET6 */ 1177fb59c426SYoshinobu Inoue 1178cfe8b629SGarrett Wollman /* 1179b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1180b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1181b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1182b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1183b8af5dfaSRobert Watson * from Linux. 1184b8af5dfaSRobert Watson */ 1185b8af5dfaSRobert Watson static void 1186ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1187b8af5dfaSRobert Watson { 1188b8af5dfaSRobert Watson 11898501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1190b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1191b8af5dfaSRobert Watson 1192b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1193b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1194b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 11953529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 1196b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1197b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1198b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1199b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1200b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1201b8af5dfaSRobert Watson } 12021baaf834SBruce M Simpson 120343d94734SJohn Baldwin ti->tcpi_rto = tp->t_rxtcur * tick; 120443d94734SJohn Baldwin ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick; 12051baaf834SBruce M Simpson ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 12061baaf834SBruce M Simpson ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 12071baaf834SBruce M Simpson 1208b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1209b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1210b8af5dfaSRobert Watson 1211b8af5dfaSRobert Watson /* 1212b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1213b8af5dfaSRobert Watson */ 1214c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1215535fbad6SKip Macy ti->tcpi_rcv_nxt = tp->rcv_nxt; 1216b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 1217b8af5dfaSRobert Watson ti->tcpi_snd_bwnd = tp->snd_bwnd; 1218535fbad6SKip Macy ti->tcpi_snd_nxt = tp->snd_nxt; 121943d94734SJohn Baldwin ti->tcpi_snd_mss = tp->t_maxseg; 122043d94734SJohn Baldwin ti->tcpi_rcv_mss = tp->t_maxseg; 1221535fbad6SKip Macy if (tp->t_flags & TF_TOE) 1222535fbad6SKip Macy ti->tcpi_options |= TCPI_OPT_TOE; 1223b8af5dfaSRobert Watson } 1224b8af5dfaSRobert Watson 1225b8af5dfaSRobert Watson /* 12261e8f5ffaSRobert Watson * tcp_ctloutput() must drop the inpcb lock before performing copyin on 12271e8f5ffaSRobert Watson * socket option arguments. When it re-acquires the lock after the copy, it 12281e8f5ffaSRobert Watson * has to revalidate that the connection is still valid for the socket 12291e8f5ffaSRobert Watson * option. 1230cfe8b629SGarrett Wollman */ 12318501a69cSRobert Watson #define INP_WLOCK_RECHECK(inp) do { \ 12328501a69cSRobert Watson INP_WLOCK(inp); \ 1233ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 12348501a69cSRobert Watson INP_WUNLOCK(inp); \ 12351e8f5ffaSRobert Watson return (ECONNRESET); \ 12361e8f5ffaSRobert Watson } \ 12371e8f5ffaSRobert Watson tp = intotcpcb(inp); \ 12381e8f5ffaSRobert Watson } while(0) 12391e8f5ffaSRobert Watson 1240df8bae1dSRodney W. Grimes int 1241ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1242df8bae1dSRodney W. Grimes { 12433f9d1ef9SRobert Watson int error, opt, optval; 1244df8bae1dSRodney W. Grimes struct inpcb *inp; 1245cfe8b629SGarrett Wollman struct tcpcb *tp; 1246b8af5dfaSRobert Watson struct tcp_info ti; 1247df8bae1dSRodney W. Grimes 1248cfe8b629SGarrett Wollman error = 0; 1249df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1250623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 12518501a69cSRobert Watson INP_WLOCK(inp); 1252cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 1253fb59c426SYoshinobu Inoue #ifdef INET6 12545cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 12558501a69cSRobert Watson INP_WUNLOCK(inp); 1256fb59c426SYoshinobu Inoue error = ip6_ctloutput(so, sopt); 12571e8f5ffaSRobert Watson } else { 1258fb59c426SYoshinobu Inoue #endif /* INET6 */ 12598501a69cSRobert Watson INP_WUNLOCK(inp); 1260cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 12611e8f5ffaSRobert Watson #ifdef INET6 12621e8f5ffaSRobert Watson } 12631e8f5ffaSRobert Watson #endif 1264df8bae1dSRodney W. Grimes return (error); 1265df8bae1dSRodney W. Grimes } 1266ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 12678501a69cSRobert Watson INP_WUNLOCK(inp); 12681e8f5ffaSRobert Watson return (ECONNRESET); 1269623dce13SRobert Watson } 1270df8bae1dSRodney W. Grimes 1271cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1272cfe8b629SGarrett Wollman case SOPT_SET: 1273cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 12741cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 127588f6b043SBruce M Simpson case TCP_MD5SIG: 12768501a69cSRobert Watson INP_WUNLOCK(inp); 12771cfd4b53SBruce M Simpson error = sooptcopyin(sopt, &optval, sizeof optval, 12781cfd4b53SBruce M Simpson sizeof optval); 12791cfd4b53SBruce M Simpson if (error) 12801e8f5ffaSRobert Watson return (error); 12811cfd4b53SBruce M Simpson 12828501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 12831cfd4b53SBruce M Simpson if (optval > 0) 12841cfd4b53SBruce M Simpson tp->t_flags |= TF_SIGNATURE; 12851cfd4b53SBruce M Simpson else 12861cfd4b53SBruce M Simpson tp->t_flags &= ~TF_SIGNATURE; 12878501a69cSRobert Watson INP_WUNLOCK(inp); 12881cfd4b53SBruce M Simpson break; 12891cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 1290df8bae1dSRodney W. Grimes case TCP_NODELAY: 1291cfe8b629SGarrett Wollman case TCP_NOOPT: 12928501a69cSRobert Watson INP_WUNLOCK(inp); 1293cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1294cfe8b629SGarrett Wollman sizeof optval); 1295cfe8b629SGarrett Wollman if (error) 12961e8f5ffaSRobert Watson return (error); 1297cfe8b629SGarrett Wollman 12988501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1299cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1300cfe8b629SGarrett Wollman case TCP_NODELAY: 1301cfe8b629SGarrett Wollman opt = TF_NODELAY; 1302cfe8b629SGarrett Wollman break; 1303cfe8b629SGarrett Wollman case TCP_NOOPT: 1304cfe8b629SGarrett Wollman opt = TF_NOOPT; 1305cfe8b629SGarrett Wollman break; 1306cfe8b629SGarrett Wollman default: 1307cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 1308cfe8b629SGarrett Wollman break; 1309cfe8b629SGarrett Wollman } 1310cfe8b629SGarrett Wollman 1311cfe8b629SGarrett Wollman if (optval) 1312cfe8b629SGarrett Wollman tp->t_flags |= opt; 1313df8bae1dSRodney W. Grimes else 1314cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 13158501a69cSRobert Watson INP_WUNLOCK(inp); 1316df8bae1dSRodney W. Grimes break; 1317df8bae1dSRodney W. Grimes 1318007581c0SJonathan Lemon case TCP_NOPUSH: 13198501a69cSRobert Watson INP_WUNLOCK(inp); 1320007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 1321007581c0SJonathan Lemon sizeof optval); 1322007581c0SJonathan Lemon if (error) 13231e8f5ffaSRobert Watson return (error); 1324007581c0SJonathan Lemon 13258501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1326007581c0SJonathan Lemon if (optval) 1327007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 1328007581c0SJonathan Lemon else { 1329007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 1330007581c0SJonathan Lemon error = tcp_output(tp); 1331007581c0SJonathan Lemon } 13328501a69cSRobert Watson INP_WUNLOCK(inp); 1333007581c0SJonathan Lemon break; 1334007581c0SJonathan Lemon 1335df8bae1dSRodney W. Grimes case TCP_MAXSEG: 13368501a69cSRobert Watson INP_WUNLOCK(inp); 1337cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1338cfe8b629SGarrett Wollman sizeof optval); 1339cfe8b629SGarrett Wollman if (error) 13401e8f5ffaSRobert Watson return (error); 1341df8bae1dSRodney W. Grimes 13428501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 134353369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 1344603724d3SBjoern A. Zeeb optval + 40 >= V_tcp_minmss) 1345cfe8b629SGarrett Wollman tp->t_maxseg = optval; 1346a0292f23SGarrett Wollman else 1347a0292f23SGarrett Wollman error = EINVAL; 13488501a69cSRobert Watson INP_WUNLOCK(inp); 1349a0292f23SGarrett Wollman break; 1350a0292f23SGarrett Wollman 1351b8af5dfaSRobert Watson case TCP_INFO: 13528501a69cSRobert Watson INP_WUNLOCK(inp); 1353b8af5dfaSRobert Watson error = EINVAL; 1354b8af5dfaSRobert Watson break; 1355b8af5dfaSRobert Watson 1356df8bae1dSRodney W. Grimes default: 13578501a69cSRobert Watson INP_WUNLOCK(inp); 1358df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1359df8bae1dSRodney W. Grimes break; 1360df8bae1dSRodney W. Grimes } 1361df8bae1dSRodney W. Grimes break; 1362df8bae1dSRodney W. Grimes 1363cfe8b629SGarrett Wollman case SOPT_GET: 13641e8f5ffaSRobert Watson tp = intotcpcb(inp); 1365cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 13661cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 136788f6b043SBruce M Simpson case TCP_MD5SIG: 13681cfd4b53SBruce M Simpson optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 13698501a69cSRobert Watson INP_WUNLOCK(inp); 1370b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 13711cfd4b53SBruce M Simpson break; 1372265ed012SBruce M Simpson #endif 13731e8f5ffaSRobert Watson 1374df8bae1dSRodney W. Grimes case TCP_NODELAY: 1375cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 13768501a69cSRobert Watson INP_WUNLOCK(inp); 1377b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1378df8bae1dSRodney W. Grimes break; 1379df8bae1dSRodney W. Grimes case TCP_MAXSEG: 1380cfe8b629SGarrett Wollman optval = tp->t_maxseg; 13818501a69cSRobert Watson INP_WUNLOCK(inp); 1382b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1383df8bae1dSRodney W. Grimes break; 1384a0292f23SGarrett Wollman case TCP_NOOPT: 1385cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 13868501a69cSRobert Watson INP_WUNLOCK(inp); 1387b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1388a0292f23SGarrett Wollman break; 1389a0292f23SGarrett Wollman case TCP_NOPUSH: 1390cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 13918501a69cSRobert Watson INP_WUNLOCK(inp); 1392b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1393b8af5dfaSRobert Watson break; 1394b8af5dfaSRobert Watson case TCP_INFO: 1395b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 13968501a69cSRobert Watson INP_WUNLOCK(inp); 1397b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 1398a0292f23SGarrett Wollman break; 1399df8bae1dSRodney W. Grimes default: 14008501a69cSRobert Watson INP_WUNLOCK(inp); 1401df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1402df8bae1dSRodney W. Grimes break; 1403df8bae1dSRodney W. Grimes } 1404df8bae1dSRodney W. Grimes break; 1405df8bae1dSRodney W. Grimes } 1406df8bae1dSRodney W. Grimes return (error); 1407df8bae1dSRodney W. Grimes } 14088501a69cSRobert Watson #undef INP_WLOCK_RECHECK 1409df8bae1dSRodney W. Grimes 141026e30fbbSDavid Greenman /* 141126e30fbbSDavid Greenman * tcp_sendspace and tcp_recvspace are the default send and receive window 141226e30fbbSDavid Greenman * sizes, respectively. These are obsolescent (this information should 141326e30fbbSDavid Greenman * be set by the route). 141426e30fbbSDavid Greenman */ 141581e561cdSDavid E. O'Brien u_long tcp_sendspace = 1024*32; 1416e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 14173d177f46SBill Fumerola &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 141881e561cdSDavid E. O'Brien u_long tcp_recvspace = 1024*64; 1419e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 14203d177f46SBill Fumerola &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1421df8bae1dSRodney W. Grimes 1422df8bae1dSRodney W. Grimes /* 1423df8bae1dSRodney W. Grimes * Attach TCP protocol to socket, allocating 1424df8bae1dSRodney W. Grimes * internet protocol control block, tcp control block, 1425df8bae1dSRodney W. Grimes * bufer space, and entering LISTEN state if to accept connections. 1426df8bae1dSRodney W. Grimes */ 14270312fbe9SPoul-Henning Kamp static int 1428ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so) 1429df8bae1dSRodney W. Grimes { 1430ad3f9ab3SAndre Oppermann struct tcpcb *tp; 1431df8bae1dSRodney W. Grimes struct inpcb *inp; 1432df8bae1dSRodney W. Grimes int error; 1433df8bae1dSRodney W. Grimes 1434df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1435df8bae1dSRodney W. Grimes error = soreserve(so, tcp_sendspace, tcp_recvspace); 1436df8bae1dSRodney W. Grimes if (error) 1437df8bae1dSRodney W. Grimes return (error); 1438df8bae1dSRodney W. Grimes } 14396741ecf5SAndre Oppermann so->so_rcv.sb_flags |= SB_AUTOSIZE; 14406741ecf5SAndre Oppermann so->so_snd.sb_flags |= SB_AUTOSIZE; 1441603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 1442603724d3SBjoern A. Zeeb error = in_pcballoc(so, &V_tcbinfo); 1443f2de87feSRobert Watson if (error) { 1444603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 1445df8bae1dSRodney W. Grimes return (error); 1446f2de87feSRobert Watson } 1447df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1448fb59c426SYoshinobu Inoue #ifdef INET6 14495cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 1450fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 1451fb59c426SYoshinobu Inoue inp->in6p_hops = -1; /* use kernel default */ 1452fb59c426SYoshinobu Inoue } 1453fb59c426SYoshinobu Inoue else 1454fb59c426SYoshinobu Inoue #endif 1455cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1456df8bae1dSRodney W. Grimes tp = tcp_newtcpcb(inp); 1457623dce13SRobert Watson if (tp == NULL) { 1458df8bae1dSRodney W. Grimes in_pcbdetach(inp); 14590206cdb8SBjoern A. Zeeb in_pcbfree(inp); 1460603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 1461df8bae1dSRodney W. Grimes return (ENOBUFS); 1462df8bae1dSRodney W. Grimes } 1463df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 14648501a69cSRobert Watson INP_WUNLOCK(inp); 1465603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 1466df8bae1dSRodney W. Grimes return (0); 1467df8bae1dSRodney W. Grimes } 1468df8bae1dSRodney W. Grimes 1469df8bae1dSRodney W. Grimes /* 1470df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 1471df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 1472df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 1473df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 1474df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 1475df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 1476df8bae1dSRodney W. Grimes */ 1477623dce13SRobert Watson static void 1478ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp) 1479df8bae1dSRodney W. Grimes { 1480e6e0b5ffSRobert Watson struct inpcb *inp = tp->t_inpcb; 1481e6e0b5ffSRobert Watson struct socket *so = inp->inp_socket; 1482e6e0b5ffSRobert Watson 1483603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 14848501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1485df8bae1dSRodney W. Grimes 1486623dce13SRobert Watson /* 1487623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 1488623dce13SRobert Watson * socket is still open. 1489623dce13SRobert Watson */ 1490623dce13SRobert Watson if (tp->t_state < TCPS_ESTABLISHED) { 1491df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1492623dce13SRobert Watson KASSERT(tp != NULL, 1493623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 1494623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 1495243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 1496623dce13SRobert Watson KASSERT(tp != NULL, 1497623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 1498623dce13SRobert Watson } else { 1499df8bae1dSRodney W. Grimes soisdisconnecting(so); 1500df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 1501623dce13SRobert Watson tcp_usrclosed(tp); 1502ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 1503bc65987aSKip Macy tcp_output_disconnect(tp); 1504df8bae1dSRodney W. Grimes } 1505df8bae1dSRodney W. Grimes } 1506df8bae1dSRodney W. Grimes 1507df8bae1dSRodney W. Grimes /* 1508df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 1509df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 1510df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1511df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 1512df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 1513df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 1514df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 1515df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 1516df8bae1dSRodney W. Grimes */ 1517623dce13SRobert Watson static void 1518ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp) 1519df8bae1dSRodney W. Grimes { 1520df8bae1dSRodney W. Grimes 1521603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 15228501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1523e6e0b5ffSRobert Watson 1524df8bae1dSRodney W. Grimes switch (tp->t_state) { 1525df8bae1dSRodney W. Grimes case TCPS_LISTEN: 1526bc65987aSKip Macy tcp_offload_listen_close(tp); 1527bc65987aSKip Macy /* FALLTHROUGH */ 1528bc65987aSKip Macy case TCPS_CLOSED: 1529df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 1530df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1531623dce13SRobert Watson /* 1532623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 1533623dce13SRobert Watson * still open. 1534623dce13SRobert Watson */ 1535623dce13SRobert Watson KASSERT(tp != NULL, 1536623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 1537df8bae1dSRodney W. Grimes break; 1538df8bae1dSRodney W. Grimes 1539a0292f23SGarrett Wollman case TCPS_SYN_SENT: 1540df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1541a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 1542a0292f23SGarrett Wollman break; 1543a0292f23SGarrett Wollman 1544df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1545df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_1; 1546df8bae1dSRodney W. Grimes break; 1547df8bae1dSRodney W. Grimes 1548df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1549df8bae1dSRodney W. Grimes tp->t_state = TCPS_LAST_ACK; 1550df8bae1dSRodney W. Grimes break; 1551df8bae1dSRodney W. Grimes } 1552abc7d910SRobert Watson if (tp->t_state >= TCPS_FIN_WAIT_2) { 1553df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 1554abc7d910SRobert Watson /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 15557c72af87SMohan Srinivasan if (tp->t_state == TCPS_FIN_WAIT_2) { 15567c72af87SMohan Srinivasan int timeout; 15577c72af87SMohan Srinivasan 15587c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 15597c72af87SMohan Srinivasan tcp_finwait2_timeout : tcp_maxidle; 1560b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 1561b6239c4aSAndras Olah } 1562df8bae1dSRodney W. Grimes } 15637c72af87SMohan Srinivasan } 1564497057eeSRobert Watson 1565497057eeSRobert Watson #ifdef DDB 1566497057eeSRobert Watson static void 1567497057eeSRobert Watson db_print_indent(int indent) 1568497057eeSRobert Watson { 1569497057eeSRobert Watson int i; 1570497057eeSRobert Watson 1571497057eeSRobert Watson for (i = 0; i < indent; i++) 1572497057eeSRobert Watson db_printf(" "); 1573497057eeSRobert Watson } 1574497057eeSRobert Watson 1575497057eeSRobert Watson static void 1576497057eeSRobert Watson db_print_tstate(int t_state) 1577497057eeSRobert Watson { 1578497057eeSRobert Watson 1579497057eeSRobert Watson switch (t_state) { 1580497057eeSRobert Watson case TCPS_CLOSED: 1581497057eeSRobert Watson db_printf("TCPS_CLOSED"); 1582497057eeSRobert Watson return; 1583497057eeSRobert Watson 1584497057eeSRobert Watson case TCPS_LISTEN: 1585497057eeSRobert Watson db_printf("TCPS_LISTEN"); 1586497057eeSRobert Watson return; 1587497057eeSRobert Watson 1588497057eeSRobert Watson case TCPS_SYN_SENT: 1589497057eeSRobert Watson db_printf("TCPS_SYN_SENT"); 1590497057eeSRobert Watson return; 1591497057eeSRobert Watson 1592497057eeSRobert Watson case TCPS_SYN_RECEIVED: 1593497057eeSRobert Watson db_printf("TCPS_SYN_RECEIVED"); 1594497057eeSRobert Watson return; 1595497057eeSRobert Watson 1596497057eeSRobert Watson case TCPS_ESTABLISHED: 1597497057eeSRobert Watson db_printf("TCPS_ESTABLISHED"); 1598497057eeSRobert Watson return; 1599497057eeSRobert Watson 1600497057eeSRobert Watson case TCPS_CLOSE_WAIT: 1601497057eeSRobert Watson db_printf("TCPS_CLOSE_WAIT"); 1602497057eeSRobert Watson return; 1603497057eeSRobert Watson 1604497057eeSRobert Watson case TCPS_FIN_WAIT_1: 1605497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_1"); 1606497057eeSRobert Watson return; 1607497057eeSRobert Watson 1608497057eeSRobert Watson case TCPS_CLOSING: 1609497057eeSRobert Watson db_printf("TCPS_CLOSING"); 1610497057eeSRobert Watson return; 1611497057eeSRobert Watson 1612497057eeSRobert Watson case TCPS_LAST_ACK: 1613497057eeSRobert Watson db_printf("TCPS_LAST_ACK"); 1614497057eeSRobert Watson return; 1615497057eeSRobert Watson 1616497057eeSRobert Watson case TCPS_FIN_WAIT_2: 1617497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_2"); 1618497057eeSRobert Watson return; 1619497057eeSRobert Watson 1620497057eeSRobert Watson case TCPS_TIME_WAIT: 1621497057eeSRobert Watson db_printf("TCPS_TIME_WAIT"); 1622497057eeSRobert Watson return; 1623497057eeSRobert Watson 1624497057eeSRobert Watson default: 1625497057eeSRobert Watson db_printf("unknown"); 1626497057eeSRobert Watson return; 1627497057eeSRobert Watson } 1628497057eeSRobert Watson } 1629497057eeSRobert Watson 1630497057eeSRobert Watson static void 1631497057eeSRobert Watson db_print_tflags(u_int t_flags) 1632497057eeSRobert Watson { 1633497057eeSRobert Watson int comma; 1634497057eeSRobert Watson 1635497057eeSRobert Watson comma = 0; 1636497057eeSRobert Watson if (t_flags & TF_ACKNOW) { 1637497057eeSRobert Watson db_printf("%sTF_ACKNOW", comma ? ", " : ""); 1638497057eeSRobert Watson comma = 1; 1639497057eeSRobert Watson } 1640497057eeSRobert Watson if (t_flags & TF_DELACK) { 1641497057eeSRobert Watson db_printf("%sTF_DELACK", comma ? ", " : ""); 1642497057eeSRobert Watson comma = 1; 1643497057eeSRobert Watson } 1644497057eeSRobert Watson if (t_flags & TF_NODELAY) { 1645497057eeSRobert Watson db_printf("%sTF_NODELAY", comma ? ", " : ""); 1646497057eeSRobert Watson comma = 1; 1647497057eeSRobert Watson } 1648497057eeSRobert Watson if (t_flags & TF_NOOPT) { 1649497057eeSRobert Watson db_printf("%sTF_NOOPT", comma ? ", " : ""); 1650497057eeSRobert Watson comma = 1; 1651497057eeSRobert Watson } 1652497057eeSRobert Watson if (t_flags & TF_SENTFIN) { 1653497057eeSRobert Watson db_printf("%sTF_SENTFIN", comma ? ", " : ""); 1654497057eeSRobert Watson comma = 1; 1655497057eeSRobert Watson } 1656497057eeSRobert Watson if (t_flags & TF_REQ_SCALE) { 1657497057eeSRobert Watson db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 1658497057eeSRobert Watson comma = 1; 1659497057eeSRobert Watson } 1660497057eeSRobert Watson if (t_flags & TF_RCVD_SCALE) { 1661497057eeSRobert Watson db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 1662497057eeSRobert Watson comma = 1; 1663497057eeSRobert Watson } 1664497057eeSRobert Watson if (t_flags & TF_REQ_TSTMP) { 1665497057eeSRobert Watson db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 1666497057eeSRobert Watson comma = 1; 1667497057eeSRobert Watson } 1668497057eeSRobert Watson if (t_flags & TF_RCVD_TSTMP) { 1669497057eeSRobert Watson db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 1670497057eeSRobert Watson comma = 1; 1671497057eeSRobert Watson } 1672497057eeSRobert Watson if (t_flags & TF_SACK_PERMIT) { 1673497057eeSRobert Watson db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 1674497057eeSRobert Watson comma = 1; 1675497057eeSRobert Watson } 1676497057eeSRobert Watson if (t_flags & TF_NEEDSYN) { 1677497057eeSRobert Watson db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 1678497057eeSRobert Watson comma = 1; 1679497057eeSRobert Watson } 1680497057eeSRobert Watson if (t_flags & TF_NEEDFIN) { 1681497057eeSRobert Watson db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 1682497057eeSRobert Watson comma = 1; 1683497057eeSRobert Watson } 1684497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 1685497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 1686497057eeSRobert Watson comma = 1; 1687497057eeSRobert Watson } 1688497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 1689497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 1690497057eeSRobert Watson comma = 1; 1691497057eeSRobert Watson } 1692497057eeSRobert Watson if (t_flags & TF_MORETOCOME) { 1693497057eeSRobert Watson db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 1694497057eeSRobert Watson comma = 1; 1695497057eeSRobert Watson } 1696497057eeSRobert Watson if (t_flags & TF_LQ_OVERFLOW) { 1697497057eeSRobert Watson db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 1698497057eeSRobert Watson comma = 1; 1699497057eeSRobert Watson } 1700497057eeSRobert Watson if (t_flags & TF_LASTIDLE) { 1701497057eeSRobert Watson db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 1702497057eeSRobert Watson comma = 1; 1703497057eeSRobert Watson } 1704497057eeSRobert Watson if (t_flags & TF_RXWIN0SENT) { 1705497057eeSRobert Watson db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 1706497057eeSRobert Watson comma = 1; 1707497057eeSRobert Watson } 1708497057eeSRobert Watson if (t_flags & TF_FASTRECOVERY) { 1709497057eeSRobert Watson db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 1710497057eeSRobert Watson comma = 1; 1711497057eeSRobert Watson } 1712497057eeSRobert Watson if (t_flags & TF_WASFRECOVERY) { 1713497057eeSRobert Watson db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 1714497057eeSRobert Watson comma = 1; 1715497057eeSRobert Watson } 1716497057eeSRobert Watson if (t_flags & TF_SIGNATURE) { 1717497057eeSRobert Watson db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 1718497057eeSRobert Watson comma = 1; 1719497057eeSRobert Watson } 1720497057eeSRobert Watson if (t_flags & TF_FORCEDATA) { 1721497057eeSRobert Watson db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 1722497057eeSRobert Watson comma = 1; 1723497057eeSRobert Watson } 1724497057eeSRobert Watson if (t_flags & TF_TSO) { 1725497057eeSRobert Watson db_printf("%sTF_TSO", comma ? ", " : ""); 1726497057eeSRobert Watson comma = 1; 1727497057eeSRobert Watson } 1728f2512ba1SRui Paulo if (t_flags & TF_ECN_PERMIT) { 1729f2512ba1SRui Paulo db_printf("%sTF_ECN_PERMIT", comma ? ", " : ""); 1730f2512ba1SRui Paulo comma = 1; 1731f2512ba1SRui Paulo } 1732497057eeSRobert Watson } 1733497057eeSRobert Watson 1734497057eeSRobert Watson static void 1735497057eeSRobert Watson db_print_toobflags(char t_oobflags) 1736497057eeSRobert Watson { 1737497057eeSRobert Watson int comma; 1738497057eeSRobert Watson 1739497057eeSRobert Watson comma = 0; 1740497057eeSRobert Watson if (t_oobflags & TCPOOB_HAVEDATA) { 1741497057eeSRobert Watson db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 1742497057eeSRobert Watson comma = 1; 1743497057eeSRobert Watson } 1744497057eeSRobert Watson if (t_oobflags & TCPOOB_HADDATA) { 1745497057eeSRobert Watson db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 1746497057eeSRobert Watson comma = 1; 1747497057eeSRobert Watson } 1748497057eeSRobert Watson } 1749497057eeSRobert Watson 1750497057eeSRobert Watson static void 1751497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 1752497057eeSRobert Watson { 1753497057eeSRobert Watson 1754497057eeSRobert Watson db_print_indent(indent); 1755497057eeSRobert Watson db_printf("%s at %p\n", name, tp); 1756497057eeSRobert Watson 1757497057eeSRobert Watson indent += 2; 1758497057eeSRobert Watson 1759497057eeSRobert Watson db_print_indent(indent); 1760497057eeSRobert Watson db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 1761497057eeSRobert Watson LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 1762497057eeSRobert Watson 1763497057eeSRobert Watson db_print_indent(indent); 176485d94372SRobert Watson db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 1765e2f2059fSMike Silbersack &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 1766497057eeSRobert Watson 1767497057eeSRobert Watson db_print_indent(indent); 1768e2f2059fSMike Silbersack db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 1769e2f2059fSMike Silbersack &tp->t_timers->tt_delack, tp->t_inpcb); 1770497057eeSRobert Watson 1771497057eeSRobert Watson db_print_indent(indent); 1772497057eeSRobert Watson db_printf("t_state: %d (", tp->t_state); 1773497057eeSRobert Watson db_print_tstate(tp->t_state); 1774497057eeSRobert Watson db_printf(")\n"); 1775497057eeSRobert Watson 1776497057eeSRobert Watson db_print_indent(indent); 1777497057eeSRobert Watson db_printf("t_flags: 0x%x (", tp->t_flags); 1778497057eeSRobert Watson db_print_tflags(tp->t_flags); 1779497057eeSRobert Watson db_printf(")\n"); 1780497057eeSRobert Watson 1781497057eeSRobert Watson db_print_indent(indent); 1782497057eeSRobert Watson db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 1783497057eeSRobert Watson tp->snd_una, tp->snd_max, tp->snd_nxt); 1784497057eeSRobert Watson 1785497057eeSRobert Watson db_print_indent(indent); 1786497057eeSRobert Watson db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 1787497057eeSRobert Watson tp->snd_up, tp->snd_wl1, tp->snd_wl2); 1788497057eeSRobert Watson 1789497057eeSRobert Watson db_print_indent(indent); 1790497057eeSRobert Watson db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 1791497057eeSRobert Watson tp->iss, tp->irs, tp->rcv_nxt); 1792497057eeSRobert Watson 1793497057eeSRobert Watson db_print_indent(indent); 1794497057eeSRobert Watson db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n", 1795497057eeSRobert Watson tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 1796497057eeSRobert Watson 1797497057eeSRobert Watson db_print_indent(indent); 1798497057eeSRobert Watson db_printf("snd_wnd: %lu snd_cwnd: %lu snd_bwnd: %lu\n", 1799497057eeSRobert Watson tp->snd_wnd, tp->snd_cwnd, tp->snd_bwnd); 1800497057eeSRobert Watson 1801497057eeSRobert Watson db_print_indent(indent); 1802497057eeSRobert Watson db_printf("snd_ssthresh: %lu snd_bandwidth: %lu snd_recover: " 1803497057eeSRobert Watson "0x%08x\n", tp->snd_ssthresh, tp->snd_bandwidth, 1804497057eeSRobert Watson tp->snd_recover); 1805497057eeSRobert Watson 1806497057eeSRobert Watson db_print_indent(indent); 18079f78a87aSJohn Baldwin db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n", 1808497057eeSRobert Watson tp->t_maxopd, tp->t_rcvtime, tp->t_starttime); 1809497057eeSRobert Watson 1810497057eeSRobert Watson db_print_indent(indent); 18119f78a87aSJohn Baldwin db_printf("t_rttime: %u t_rtsq: 0x%08x t_bw_rtttime: %u\n", 1812497057eeSRobert Watson tp->t_rtttime, tp->t_rtseq, tp->t_bw_rtttime); 1813497057eeSRobert Watson 1814497057eeSRobert Watson db_print_indent(indent); 1815497057eeSRobert Watson db_printf("t_bw_rtseq: 0x%08x t_rxtcur: %d t_maxseg: %u " 1816497057eeSRobert Watson "t_srtt: %d\n", tp->t_bw_rtseq, tp->t_rxtcur, tp->t_maxseg, 1817497057eeSRobert Watson tp->t_srtt); 1818497057eeSRobert Watson 1819497057eeSRobert Watson db_print_indent(indent); 1820497057eeSRobert Watson db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 1821497057eeSRobert Watson "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 1822497057eeSRobert Watson tp->t_rttbest); 1823497057eeSRobert Watson 1824497057eeSRobert Watson db_print_indent(indent); 1825497057eeSRobert Watson db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n", 1826497057eeSRobert Watson tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 1827497057eeSRobert Watson 1828497057eeSRobert Watson db_print_indent(indent); 1829497057eeSRobert Watson db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 1830497057eeSRobert Watson db_print_toobflags(tp->t_oobflags); 1831497057eeSRobert Watson db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 1832497057eeSRobert Watson 1833497057eeSRobert Watson db_print_indent(indent); 1834497057eeSRobert Watson db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 1835497057eeSRobert Watson tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 1836497057eeSRobert Watson 1837497057eeSRobert Watson db_print_indent(indent); 18389f78a87aSJohn Baldwin db_printf("ts_recent: %u ts_recent_age: %u\n", 18391a553740SAndre Oppermann tp->ts_recent, tp->ts_recent_age); 1840497057eeSRobert Watson 1841497057eeSRobert Watson db_print_indent(indent); 1842497057eeSRobert Watson db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 1843497057eeSRobert Watson "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 1844497057eeSRobert Watson 1845497057eeSRobert Watson db_print_indent(indent); 1846497057eeSRobert Watson db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x " 18479f78a87aSJohn Baldwin "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 1848497057eeSRobert Watson tp->snd_recover_prev, tp->t_badrxtwin); 1849497057eeSRobert Watson 1850497057eeSRobert Watson db_print_indent(indent); 18513529149eSAndre Oppermann db_printf("snd_numholes: %d snd_holes first: %p\n", 18523529149eSAndre Oppermann tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 1853497057eeSRobert Watson 1854497057eeSRobert Watson db_print_indent(indent); 1855497057eeSRobert Watson db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: " 1856497057eeSRobert Watson "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata); 1857497057eeSRobert Watson 1858497057eeSRobert Watson /* Skip sackblks, sackhint. */ 1859497057eeSRobert Watson 1860497057eeSRobert Watson db_print_indent(indent); 1861497057eeSRobert Watson db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 1862497057eeSRobert Watson tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 1863497057eeSRobert Watson } 1864497057eeSRobert Watson 1865497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 1866497057eeSRobert Watson { 1867497057eeSRobert Watson struct tcpcb *tp; 1868497057eeSRobert Watson 1869497057eeSRobert Watson if (!have_addr) { 1870497057eeSRobert Watson db_printf("usage: show tcpcb <addr>\n"); 1871497057eeSRobert Watson return; 1872497057eeSRobert Watson } 1873497057eeSRobert Watson tp = (struct tcpcb *)addr; 1874497057eeSRobert Watson 1875497057eeSRobert Watson db_print_tcpcb(tp, "tcpcb", 0); 1876497057eeSRobert Watson } 1877497057eeSRobert Watson #endif 1878