tcp_usrreq.c revision 218271
1110476Strhodes/*- 266830Sobrien * Copyright (c) 1982, 1986, 1988, 1993 366830Sobrien * The Regents of the University of California. 466830Sobrien * Copyright (c) 2006-2007 Robert N. M. Watson 566830Sobrien * All rights reserved. 666830Sobrien * 766830Sobrien * Redistribution and use in source and binary forms, with or without 866830Sobrien * modification, are permitted provided that the following conditions 966830Sobrien * are met: 1066830Sobrien * 1. Redistributions of source code must retain the above copyright 1166830Sobrien * notice, this list of conditions and the following disclaimer. 1266830Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1366830Sobrien * notice, this list of conditions and the following disclaimer in the 1466830Sobrien * documentation and/or other materials provided with the distribution. 1566830Sobrien * 4. Neither the name of the University nor the names of its contributors 1666830Sobrien * may be used to endorse or promote products derived from this software 1766830Sobrien * without specific prior written permission. 1866830Sobrien * 1966830Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2066830Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2166830Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2266830Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2366830Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2466830Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2566830Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2650472Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2766830Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2815027Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2966830Sobrien * SUCH DAMAGE. 30168384Srwatson * 3166830Sobrien * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 3266830Sobrien */ 3343849Sjkh 3481618Sdd#include <sys/cdefs.h> 3581618Sdd__FBSDID("$FreeBSD: head/sys/netinet/tcp_usrreq.c 218271 2011-02-04 14:13:15Z jhb $"); 3681618Sdd 3781618Sdd#include "opt_ddb.h" 3881618Sdd#include "opt_inet.h" 3981618Sdd#include "opt_inet6.h" 4081618Sdd#include "opt_tcpdebug.h" 4133203Sadam 4233203Sadam#include <sys/param.h> 4315027Sphk#include <sys/systm.h> 4429300Sdanny#include <sys/malloc.h> 45163749Sphk#include <sys/kernel.h> 46163749Sphk#include <sys/sysctl.h> 47163749Sphk#include <sys/mbuf.h> 48163749Sphk#ifdef INET6 49229783Suqs#include <sys/domain.h> 50163749Sphk#endif /* INET6 */ 51163749Sphk#include <sys/socket.h> 52163749Sphk#include <sys/socketvar.h> 5315027Sphk#include <sys/protosw.h> 5451231Ssheldonh#include <sys/proc.h> 5529300Sdanny#include <sys/jail.h> 5615027Sphk 5715027Sphk#ifdef DDB 5815027Sphk#include <ddb/ddb.h> 5915027Sphk#endif 6015027Sphk 6115027Sphk#include <net/if.h> 6273023Sdes#include <net/route.h> 6315210Sphk#include <net/vnet.h> 6415210Sphk 6515210Sphk#include <netinet/cc.h> 6673023Sdes#include <netinet/in.h> 6725478Sjkh#include <netinet/in_systm.h> 6873023Sdes#ifdef INET6 6915210Sphk#include <netinet/ip6.h> 7015210Sphk#endif 7115210Sphk#include <netinet/in_pcb.h> 72175244Smaxim#ifdef INET6 73175244Smaxim#include <netinet6/in6_pcb.h> 7415027Sphk#endif 75175244Smaxim#include <netinet/in_var.h> 76175244Smaxim#include <netinet/ip_var.h> 77175244Smaxim#ifdef INET6 78175244Smaxim#include <netinet6/ip6_var.h> 7915027Sphk#include <netinet6/scope6_var.h> 8015027Sphk#endif 81238416Skevlo#include <netinet/tcp_fsm.h> 8291019Scjc#include <netinet/tcp_seq.h> 8391019Scjc#include <netinet/tcp_timer.h> 8491019Scjc#include <netinet/tcp_var.h> 8591019Scjc#include <netinet/tcpip.h> 8691019Scjc#ifdef TCPDEBUG 8791019Scjc#include <netinet/tcp_debug.h> 88200028Sume#endif 89200028Sume#include <netinet/tcp_offload.h> 90200028Sume 91200028Sume/* 9291019Scjc * TCP protocol interface to socket abstraction. 9391019Scjc */ 94238416Skevlostatic int tcp_attach(struct socket *); 95200028Sumestatic int tcp_connect(struct tcpcb *, struct sockaddr *, 96200028Sume struct thread *td); 97200028Sume#ifdef INET6 98200028Sumestatic int tcp6_connect(struct tcpcb *, struct sockaddr *, 99200028Sume struct thread *td); 100200028Sume#endif /* INET6 */ 101200028Sumestatic void tcp_disconnect(struct tcpcb *); 102200028Sumestatic void tcp_usrclosed(struct tcpcb *); 103200028Sumestatic void tcp_fill_info(struct tcpcb *, struct tcp_info *); 104200028Sume 105200028Sume#ifdef TCPDEBUG 106200028Sume#define TCPDEBUG0 int ostate = 0 107200028Sume#define TCPDEBUG1() ostate = tp ? tp->t_state : 0 108229783Suqs#define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 109200028Sume tcp_trace(TA_USER, ostate, tp, 0, 0, req) 110200028Sume#else 111200028Sume#define TCPDEBUG0 112200028Sume#define TCPDEBUG1() 113200028Sume#define TCPDEBUG2(req) 114200028Sume#endif 11551231Ssheldonh 11651231Ssheldonh/* 11729300Sdanny * TCP attaches to socket via pru_attach(), reserving space, 11829300Sdanny * and an internet control block. 119200028Sume */ 120200028Sumestatic int 121200028Sumetcp_usr_attach(struct socket *so, int proto, struct thread *td) 122200028Sume{ 123200028Sume struct inpcb *inp; 12415027Sphk struct tcpcb *tp = NULL; 12529300Sdanny int error; 12651231Ssheldonh TCPDEBUG0; 12751231Ssheldonh 12851231Ssheldonh inp = sotoinpcb(so); 12929300Sdanny KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 13051231Ssheldonh TCPDEBUG1(); 13151231Ssheldonh 13229300Sdanny error = tcp_attach(so); 13351231Ssheldonh if (error) 13451231Ssheldonh goto out; 13529300Sdanny 13629300Sdanny if ((so->so_options & SO_LINGER) && so->so_linger == 0) 13716578Salex so->so_linger = TCP_LINGERTIME; 13851231Ssheldonh 13950357Ssheldonh inp = sotoinpcb(so); 14016578Salex tp = intotcpcb(inp); 141163749Sphkout: 142200028Sume TCPDEBUG2(PRU_ATTACH); 143163749Sphk return error; 14416578Salex} 14564244Sru 14664244Sru/* 14764244Sru * tcp_detach is called when the socket layer loses its final reference 14864244Sru * to the socket, be it a file descriptor reference, a reference from TCP, 14951231Ssheldonh * etc. At this point, there is only one case in which we will keep around 15064244Sru * inpcb state: time wait. 15164244Sru * 15273842Sobrien * This function can probably be re-absorbed back into tcp_usr_detach() now 15364244Sru * that there is a single detach path. 15465257Sru */ 15564244Srustatic void 15664244Srutcp_detach(struct socket *so, struct inpcb *inp) 15764244Sru{ 158152562Sume struct tcpcb *tp; 15964244Sru 16064244Sru INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 16164244Sru INP_WLOCK_ASSERT(inp); 162165648Spiso 163165648Spiso KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp")); 164165648Spiso KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so")); 165175522Srafan 166175522Srafan tp = intotcpcb(inp); 167175522Srafan 168175522Srafan if (inp->inp_flags & INP_TIMEWAIT) { 169175522Srafan /* 170175522Srafan * There are two cases to handle: one in which the time wait 171175522Srafan * state is being discarded (INP_DROPPED), and one in which 172165648Spiso * this connection will remain in timewait. In the former, 173165648Spiso * it is time to discard all state (except tcptw, which has 174165648Spiso * already been discarded by the timewait close code, which 175165648Spiso * should be further up the call stack somewhere). In the 17651231Ssheldonh * latter case, we detach from the socket, but leave the pcb 17735267Sbrian * present until timewait ends. 17835267Sbrian * 17915027Sphk * XXXRW: Would it be cleaner to free the tcptw here? 18015027Sphk */ 18151805Smpp if (inp->inp_flags & INP_DROPPED) { 18217594Sjkh KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && " 18351231Ssheldonh "INP_DROPPED && tp != NULL")); 18450357Ssheldonh in_pcbdetach(inp); 18515027Sphk in_pcbfree(inp); 18615027Sphk } else { 18717594Sjkh in_pcbdetach(inp); 18851231Ssheldonh INP_WUNLOCK(inp); 18951231Ssheldonh } 19051231Ssheldonh } else { 19150357Ssheldonh /* 19251231Ssheldonh * If the connection is not in timewait, we consider two 19354108Sobrien * two conditions: one in which no further processing is 19451231Ssheldonh * necessary (dropped || embryonic), and one in which TCP is 19551231Ssheldonh * not yet done, but no longer requires the socket, so the 19651231Ssheldonh * pcb will persist for the time being. 19751231Ssheldonh * 198181762Sjhb * XXXRW: Does the second case still occur? 199181762Sjhb */ 200200028Sume if (inp->inp_flags & INP_DROPPED || 201200028Sume tp->t_state < TCPS_SYN_SENT) { 20251231Ssheldonh tcp_discardcb(tp); 20329300Sdanny in_pcbdetach(inp); 204181762Sjhb in_pcbfree(inp); 205181762Sjhb } else 206200028Sume in_pcbdetach(inp); 20717594Sjkh } 208179598Skeramida} 209181761Sjhb 210179598Skeramida/* 21151231Ssheldonh * pru_detach() detaches the TCP protocol from the socket. 212181761Sjhb * If the protocol state is non-embryonic, then can't 213181761Sjhb * do this directly: have to initiate a pru_disconnect(), 214200028Sume * which may finish later; embryonic TCB's can just 215202460Sume * be discarded here. 216202460Sume */ 217200028Sumestatic void 21815027Sphktcp_usr_detach(struct socket *so) 219200028Sume{ 220200028Sume struct inpcb *inp; 221200028Sume 222200028Sume inp = sotoinpcb(so); 223201930Sume KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL")); 224202460Sume INP_INFO_WLOCK(&V_tcbinfo); 225200028Sume INP_WLOCK(inp); 226200028Sume KASSERT(inp->inp_socket != NULL, 22751231Ssheldonh ("tcp_usr_detach: inp_socket == NULL")); 22851231Ssheldonh tcp_detach(so, inp); 22915027Sphk INP_INFO_WUNLOCK(&V_tcbinfo); 23052873Sru} 23152873Sru 23252873Sru/* 23351231Ssheldonh * Give the socket an address. 234163749Sphk */ 23515027Sphkstatic int 23651231Ssheldonhtcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 237163749Sphk{ 23815027Sphk int error = 0; 23951231Ssheldonh struct inpcb *inp; 24051231Ssheldonh struct tcpcb *tp = NULL; 24115027Sphk struct sockaddr_in *sinp; 24251231Ssheldonh 243163749Sphk sinp = (struct sockaddr_in *)nam; 24415027Sphk if (nam->sa_len != sizeof (*sinp)) 24551231Ssheldonh return (EINVAL); 246163749Sphk /* 24715027Sphk * Must check for multicast addresses and disallow binding 24851231Ssheldonh * to them. 24951231Ssheldonh */ 25051231Ssheldonh if (sinp->sin_family == AF_INET && 25151231Ssheldonh IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 25215027Sphk return (EAFNOSUPPORT); 25351231Ssheldonh 25451231Ssheldonh TCPDEBUG0; 25551231Ssheldonh INP_INFO_WLOCK(&V_tcbinfo); 256121881Sru inp = sotoinpcb(so); 25751231Ssheldonh KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 258181762Sjhb INP_WLOCK(inp); 259181762Sjhb if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 260200028Sume error = EINVAL; 261200028Sume goto out; 262200028Sume } 263200028Sume tp = intotcpcb(inp); 264200028Sume TCPDEBUG1(); 265200028Sume error = in_pcbbind(inp, nam, td->td_ucred); 266200028Sumeout: 267200028Sume TCPDEBUG2(PRU_BIND); 26851231Ssheldonh INP_WUNLOCK(inp); 269284691Sjulian INP_INFO_WUNLOCK(&V_tcbinfo); 27015027Sphk 271181761Sjhb return (error); 272181762Sjhb} 273181762Sjhb 274200028Sume#ifdef INET6 275200028Sumestatic int 27617594Sjkhtcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 277181761Sjhb{ 278181762Sjhb int error = 0; 279181762Sjhb struct inpcb *inp; 280200028Sume struct tcpcb *tp = NULL; 281200028Sume struct sockaddr_in6 *sin6p; 28215027Sphk 28351231Ssheldonh sin6p = (struct sockaddr_in6 *)nam; 284181761Sjhb if (nam->sa_len != sizeof (*sin6p)) 285181761Sjhb return (EINVAL); 286200028Sume /* 287200028Sume * Must check for multicast addresses and disallow binding 288200028Sume * to them. 289200028Sume */ 290200028Sume if (sin6p->sin6_family == AF_INET6 && 291200028Sume IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 292200028Sume return (EAFNOSUPPORT); 29315027Sphk 294284920Sjulian TCPDEBUG0; 29551231Ssheldonh INP_INFO_WLOCK(&V_tcbinfo); 296284691Sjulian inp = sotoinpcb(so); 297284691Sjulian KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 298284691Sjulian INP_WLOCK(inp); 299284691Sjulian if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 30015027Sphk error = EINVAL; 301284920Sjulian goto out; 30264028Sobrien } 30364028Sobrien tp = intotcpcb(inp); 304284691Sjulian TCPDEBUG1(); 305284691Sjulian inp->inp_vflag &= ~INP_IPV4; 306284691Sjulian inp->inp_vflag |= INP_IPV6; 307284691Sjulian if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 308284691Sjulian if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 30956736Srgrimes inp->inp_vflag |= INP_IPV4; 310284691Sjulian else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 311284691Sjulian struct sockaddr_in sin; 31264244Sru 31364244Sru in6_sin6_2_sin(&sin, sin6p); 31464244Sru inp->inp_vflag |= INP_IPV4; 31564244Sru inp->inp_vflag &= ~INP_IPV6; 31664244Sru error = in_pcbbind(inp, (struct sockaddr *)&sin, 31764244Sru td->td_ucred); 31864244Sru goto out; 31964244Sru } 32064244Sru } 32164244Sru error = in6_pcbbind(inp, nam, td->td_ucred); 322200028Sumeout: 32364244Sru TCPDEBUG2(PRU_BIND); 32464244Sru INP_WUNLOCK(inp); 32564244Sru INP_INFO_WUNLOCK(&V_tcbinfo); 32664244Sru return (error); 327284691Sjulian} 328200028Sume#endif /* INET6 */ 329200028Sume 330200028Sume/* 331200028Sume * Prepare to accept connections. 332200028Sume */ 333200028Sumestatic int 334200028Sumetcp_usr_listen(struct socket *so, int backlog, struct thread *td) 335200028Sume{ 336200028Sume int error = 0; 337200028Sume struct inpcb *inp; 338200028Sume struct tcpcb *tp = NULL; 339200028Sume 340200028Sume TCPDEBUG0; 341200028Sume INP_INFO_WLOCK(&V_tcbinfo); 342200028Sume inp = sotoinpcb(so); 343200028Sume KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 344200028Sume INP_WLOCK(inp); 345200028Sume if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 346200028Sume error = EINVAL; 347200028Sume goto out; 348200028Sume } 349200028Sume tp = intotcpcb(inp); 350200028Sume TCPDEBUG1(); 351200028Sume SOCK_LOCK(so); 352200028Sume error = solisten_proto_check(so); 353200028Sume if (error == 0 && inp->inp_lport == 0) 354200028Sume error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 355200028Sume if (error == 0) { 356200028Sume tp->t_state = TCPS_LISTEN; 357200028Sume solisten_proto(so, backlog); 358200028Sume tcp_offload_listen_open(tp); 359200028Sume } 360200028Sume SOCK_UNLOCK(so); 361200028Sume 362200028Sumeout: 363200028Sume TCPDEBUG2(PRU_LISTEN); 364200028Sume INP_WUNLOCK(inp); 365200028Sume INP_INFO_WUNLOCK(&V_tcbinfo); 366200028Sume return (error); 367200028Sume} 368200028Sume 369200028Sume#ifdef INET6 370200028Sumestatic int 371200028Sumetcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 372200028Sume{ 373200028Sume int error = 0; 374200028Sume struct inpcb *inp; 375200028Sume struct tcpcb *tp = NULL; 376200028Sume 37751231Ssheldonh TCPDEBUG0; 37851231Ssheldonh INP_INFO_WLOCK(&V_tcbinfo); 37915027Sphk inp = sotoinpcb(so); 38052873Sru KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 38152873Sru INP_WLOCK(inp); 38252873Sru if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 38351231Ssheldonh error = EINVAL; 384181760Sjhb goto out; 38515027Sphk } 38651231Ssheldonh tp = intotcpcb(inp); 387181760Sjhb TCPDEBUG1(); 388181760Sjhb SOCK_LOCK(so); 389181760Sjhb error = solisten_proto_check(so); 39015027Sphk if (error == 0 && inp->inp_lport == 0) { 39151231Ssheldonh inp->inp_vflag &= ~INP_IPV4; 392181760Sjhb if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 39315027Sphk inp->inp_vflag |= INP_IPV4; 39451231Ssheldonh error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 395200028Sume } 396200028Sume if (error == 0) { 397200028Sume tp->t_state = TCPS_LISTEN; 398200028Sume solisten_proto(so, backlog); 399200028Sume } 40015027Sphk SOCK_UNLOCK(so); 40151231Ssheldonh 40251231Ssheldonhout: 40315027Sphk TCPDEBUG2(PRU_LISTEN); 40451231Ssheldonh INP_WUNLOCK(inp); 405181760Sjhb INP_INFO_WUNLOCK(&V_tcbinfo); 40615027Sphk return (error); 40751231Ssheldonh} 408181760Sjhb#endif /* INET6 */ 40915027Sphk 41051231Ssheldonh/* 41151231Ssheldonh * Initiate connection to peer. 41251231Ssheldonh * Create a template for use in transmissions on this connection. 41351231Ssheldonh * Enter SYN_SENT state, and mark socket as connecting. 41415027Sphk * Start keep-alive timer, and seed output sequence space. 415163749Sphk * Send initial segment on connection. 416163749Sphk */ 417273201Shrsstatic int 418273201Shrstcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 419206399Sume{ 420206479Sume int error = 0; 421163749Sphk struct inpcb *inp; 422206399Sume struct tcpcb *tp = NULL; 423206479Sume struct sockaddr_in *sinp; 424206479Sume 425206399Sume sinp = (struct sockaddr_in *)nam; 426206399Sume if (nam->sa_len != sizeof (*sinp)) 427206399Sume return (EINVAL); 428163749Sphk /* 429163749Sphk * Must disallow TCP ``connections'' to multicast addresses. 430163749Sphk */ 431163749Sphk if (sinp->sin_family == AF_INET 432229783Suqs && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 433163749Sphk return (EAFNOSUPPORT); 434200028Sume if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 435163749Sphk return (error); 436163749Sphk 437163749Sphk TCPDEBUG0; 438163749Sphk INP_INFO_WLOCK(&V_tcbinfo); 439163749Sphk inp = sotoinpcb(so); 440163749Sphk KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 441163749Sphk INP_WLOCK(inp); 442163749Sphk if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 443163749Sphk error = EINVAL; 444163749Sphk goto out; 445200028Sume } 446202460Sume tp = intotcpcb(inp); 447200028Sume TCPDEBUG1(); 448163749Sphk if ((error = tcp_connect(tp, nam, td)) != 0) 449163749Sphk goto out; 450163749Sphk error = tcp_output_connect(so, nam); 451163749Sphkout: 452163749Sphk TCPDEBUG2(PRU_CONNECT); 453200028Sume INP_WUNLOCK(inp); 454202460Sume INP_INFO_WUNLOCK(&V_tcbinfo); 455200028Sume return (error); 456208060Sdougb} 457163749Sphk 458163749Sphk#ifdef INET6 459200028Sumestatic int 460200028Sumetcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 461200028Sume{ 462163749Sphk int error = 0; 463163749Sphk struct inpcb *inp; 464163749Sphk struct tcpcb *tp = NULL; 465201752Sume struct sockaddr_in6 *sin6p; 466201752Sume 467201752Sume TCPDEBUG0; 468201752Sume 469163749Sphk sin6p = (struct sockaddr_in6 *)nam; 470163749Sphk if (nam->sa_len != sizeof (*sin6p)) 471208060Sdougb return (EINVAL); 472163749Sphk /* 473163749Sphk * Must disallow TCP ``connections'' to multicast addresses. 474163749Sphk */ 475208060Sdougb if (sin6p->sin6_family == AF_INET6 476163749Sphk && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 477163749Sphk return (EAFNOSUPPORT); 478163749Sphk 479163749Sphk INP_INFO_WLOCK(&V_tcbinfo); 480163749Sphk inp = sotoinpcb(so); 481163749Sphk KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 482273201Shrs INP_WLOCK(inp); 483273201Shrs if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 484273201Shrs error = EINVAL; 485273201Shrs goto out; 486273201Shrs } 487273201Shrs tp = intotcpcb(inp); 488273201Shrs TCPDEBUG1(); 489273201Shrs if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 490273201Shrs struct sockaddr_in sin; 491273201Shrs 492273201Shrs if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 493273301Shrs error = EINVAL; 494273301Shrs goto out; 495273201Shrs } 496273201Shrs 497273201Shrs in6_sin6_2_sin(&sin, sin6p); 498273201Shrs inp->inp_vflag |= INP_IPV4; 499273201Shrs inp->inp_vflag &= ~INP_IPV6; 500273201Shrs if ((error = prison_remote_ip4(td->td_ucred, 501163749Sphk &sin.sin_addr)) != 0) 502163749Sphk goto out; 503163749Sphk if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 504163749Sphk goto out; 505163749Sphk error = tcp_output_connect(so, nam); 506163749Sphk goto out; 507163749Sphk } 508163749Sphk inp->inp_vflag &= ~INP_IPV4; 509163749Sphk inp->inp_vflag |= INP_IPV6; 510200028Sume inp->inp_inc.inc_flags |= INC_ISIPV6; 511163749Sphk if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0) 512163749Sphk goto out; 513163749Sphk if ((error = tcp6_connect(tp, nam, td)) != 0) 514163749Sphk goto out; 515163749Sphk error = tcp_output_connect(so, nam); 516163749Sphk 517163749Sphkout: 518229783Suqs TCPDEBUG2(PRU_CONNECT); 519163749Sphk INP_WUNLOCK(inp); 520163749Sphk INP_INFO_WUNLOCK(&V_tcbinfo); 521163749Sphk return (error); 522163749Sphk} 523163749Sphk#endif /* INET6 */ 524163749Sphk 525163749Sphk/* 526229783Suqs * Initiate disconnect from peer. 527163749Sphk * If connection never passed embryonic stage, just drop; 528163749Sphk * else if don't need to let data drain, then can just drop anyways, 529163749Sphk * else have to begin TCP shutdown process: mark socket disconnecting, 530163749Sphk * drain unread data, state switch to reflect user close, and 531163749Sphk * send segment (e.g. FIN) to peer. Socket will be really disconnected 532163749Sphk * when peer sends FIN and acks ours. 533163749Sphk * 534163749Sphk * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 535163749Sphk */ 536163749Sphkstatic int 537163749Sphktcp_usr_disconnect(struct socket *so) 538163749Sphk{ 53991019Scjc struct inpcb *inp; 540163749Sphk struct tcpcb *tp = NULL; 54191019Scjc int error = 0; 54251231Ssheldonh 54351231Ssheldonh TCPDEBUG0; 54451231Ssheldonh INP_INFO_WLOCK(&V_tcbinfo); 54559669Sbsd inp = sotoinpcb(so); 54657014Spaul KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 54751231Ssheldonh INP_WLOCK(inp); 54851231Ssheldonh if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 54951231Ssheldonh error = ECONNRESET; 550 goto out; 551 } 552 tp = intotcpcb(inp); 553 TCPDEBUG1(); 554 tcp_disconnect(tp); 555out: 556 TCPDEBUG2(PRU_DISCONNECT); 557 INP_WUNLOCK(inp); 558 INP_INFO_WUNLOCK(&V_tcbinfo); 559 return (error); 560} 561 562/* 563 * Accept a connection. Essentially all the work is done at higher levels; 564 * just return the address of the peer, storing through addr. 565 * 566 * The rationale for acquiring the tcbinfo lock here is somewhat complicated, 567 * and is described in detail in the commit log entry for r175612. Acquiring 568 * it delays an accept(2) racing with sonewconn(), which inserts the socket 569 * before the inpcb address/port fields are initialized. A better fix would 570 * prevent the socket from being placed in the listen queue until all fields 571 * are fully initialized. 572 */ 573static int 574tcp_usr_accept(struct socket *so, struct sockaddr **nam) 575{ 576 int error = 0; 577 struct inpcb *inp = NULL; 578 struct tcpcb *tp = NULL; 579 struct in_addr addr; 580 in_port_t port = 0; 581 TCPDEBUG0; 582 583 if (so->so_state & SS_ISDISCONNECTED) 584 return (ECONNABORTED); 585 586 inp = sotoinpcb(so); 587 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 588 INP_INFO_RLOCK(&V_tcbinfo); 589 INP_WLOCK(inp); 590 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 591 error = ECONNABORTED; 592 goto out; 593 } 594 tp = intotcpcb(inp); 595 TCPDEBUG1(); 596 597 /* 598 * We inline in_getpeeraddr and COMMON_END here, so that we can 599 * copy the data of interest and defer the malloc until after we 600 * release the lock. 601 */ 602 port = inp->inp_fport; 603 addr = inp->inp_faddr; 604 605out: 606 TCPDEBUG2(PRU_ACCEPT); 607 INP_WUNLOCK(inp); 608 INP_INFO_RUNLOCK(&V_tcbinfo); 609 if (error == 0) 610 *nam = in_sockaddr(port, &addr); 611 return error; 612} 613 614#ifdef INET6 615static int 616tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 617{ 618 struct inpcb *inp = NULL; 619 int error = 0; 620 struct tcpcb *tp = NULL; 621 struct in_addr addr; 622 struct in6_addr addr6; 623 in_port_t port = 0; 624 int v4 = 0; 625 TCPDEBUG0; 626 627 if (so->so_state & SS_ISDISCONNECTED) 628 return (ECONNABORTED); 629 630 inp = sotoinpcb(so); 631 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 632 INP_WLOCK(inp); 633 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 634 error = ECONNABORTED; 635 goto out; 636 } 637 tp = intotcpcb(inp); 638 TCPDEBUG1(); 639 640 /* 641 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 642 * copy the data of interest and defer the malloc until after we 643 * release the lock. 644 */ 645 if (inp->inp_vflag & INP_IPV4) { 646 v4 = 1; 647 port = inp->inp_fport; 648 addr = inp->inp_faddr; 649 } else { 650 port = inp->inp_fport; 651 addr6 = inp->in6p_faddr; 652 } 653 654out: 655 TCPDEBUG2(PRU_ACCEPT); 656 INP_WUNLOCK(inp); 657 if (error == 0) { 658 if (v4) 659 *nam = in6_v4mapsin6_sockaddr(port, &addr); 660 else 661 *nam = in6_sockaddr(port, &addr6); 662 } 663 return error; 664} 665#endif /* INET6 */ 666 667/* 668 * Mark the connection as being incapable of further output. 669 */ 670static int 671tcp_usr_shutdown(struct socket *so) 672{ 673 int error = 0; 674 struct inpcb *inp; 675 struct tcpcb *tp = NULL; 676 677 TCPDEBUG0; 678 INP_INFO_WLOCK(&V_tcbinfo); 679 inp = sotoinpcb(so); 680 KASSERT(inp != NULL, ("inp == NULL")); 681 INP_WLOCK(inp); 682 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 683 error = ECONNRESET; 684 goto out; 685 } 686 tp = intotcpcb(inp); 687 TCPDEBUG1(); 688 socantsendmore(so); 689 tcp_usrclosed(tp); 690 if (!(inp->inp_flags & INP_DROPPED)) 691 error = tcp_output_disconnect(tp); 692 693out: 694 TCPDEBUG2(PRU_SHUTDOWN); 695 INP_WUNLOCK(inp); 696 INP_INFO_WUNLOCK(&V_tcbinfo); 697 698 return (error); 699} 700 701/* 702 * After a receive, possibly send window update to peer. 703 */ 704static int 705tcp_usr_rcvd(struct socket *so, int flags) 706{ 707 struct inpcb *inp; 708 struct tcpcb *tp = NULL; 709 int error = 0; 710 711 TCPDEBUG0; 712 inp = sotoinpcb(so); 713 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 714 INP_WLOCK(inp); 715 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 716 error = ECONNRESET; 717 goto out; 718 } 719 tp = intotcpcb(inp); 720 TCPDEBUG1(); 721 tcp_output_rcvd(tp); 722 723out: 724 TCPDEBUG2(PRU_RCVD); 725 INP_WUNLOCK(inp); 726 return (error); 727} 728 729/* 730 * Do a send by putting data in output queue and updating urgent 731 * marker if URG set. Possibly send more data. Unlike the other 732 * pru_*() routines, the mbuf chains are our responsibility. We 733 * must either enqueue them or free them. The other pru_* routines 734 * generally are caller-frees. 735 */ 736static int 737tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 738 struct sockaddr *nam, struct mbuf *control, struct thread *td) 739{ 740 int error = 0; 741 struct inpcb *inp; 742 struct tcpcb *tp = NULL; 743 int headlocked = 0; 744#ifdef INET6 745 int isipv6; 746#endif 747 TCPDEBUG0; 748 749 /* 750 * We require the pcbinfo lock in two cases: 751 * 752 * (1) An implied connect is taking place, which can result in 753 * binding IPs and ports and hence modification of the pcb hash 754 * chains. 755 * 756 * (2) PRUS_EOF is set, resulting in explicit close on the send. 757 */ 758 if ((nam != NULL) || (flags & PRUS_EOF)) { 759 INP_INFO_WLOCK(&V_tcbinfo); 760 headlocked = 1; 761 } 762 inp = sotoinpcb(so); 763 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 764 INP_WLOCK(inp); 765 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 766 if (control) 767 m_freem(control); 768 if (m) 769 m_freem(m); 770 error = ECONNRESET; 771 goto out; 772 } 773#ifdef INET6 774 isipv6 = nam && nam->sa_family == AF_INET6; 775#endif /* INET6 */ 776 tp = intotcpcb(inp); 777 TCPDEBUG1(); 778 if (control) { 779 /* TCP doesn't do control messages (rights, creds, etc) */ 780 if (control->m_len) { 781 m_freem(control); 782 if (m) 783 m_freem(m); 784 error = EINVAL; 785 goto out; 786 } 787 m_freem(control); /* empty control, just free it */ 788 } 789 if (!(flags & PRUS_OOB)) { 790 sbappendstream(&so->so_snd, m); 791 if (nam && tp->t_state < TCPS_SYN_SENT) { 792 /* 793 * Do implied connect if not yet connected, 794 * initialize window to default value, and 795 * initialize maxseg/maxopd using peer's cached 796 * MSS. 797 */ 798 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 799#ifdef INET6 800 if (isipv6) 801 error = tcp6_connect(tp, nam, td); 802 else 803#endif /* INET6 */ 804 error = tcp_connect(tp, nam, td); 805 if (error) 806 goto out; 807 tp->snd_wnd = TTCP_CLIENT_SND_WND; 808 tcp_mss(tp, -1); 809 } 810 if (flags & PRUS_EOF) { 811 /* 812 * Close the send side of the connection after 813 * the data is sent. 814 */ 815 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 816 socantsendmore(so); 817 tcp_usrclosed(tp); 818 } 819 if (headlocked) { 820 INP_INFO_WUNLOCK(&V_tcbinfo); 821 headlocked = 0; 822 } 823 if (!(inp->inp_flags & INP_DROPPED)) { 824 if (flags & PRUS_MORETOCOME) 825 tp->t_flags |= TF_MORETOCOME; 826 error = tcp_output_send(tp); 827 if (flags & PRUS_MORETOCOME) 828 tp->t_flags &= ~TF_MORETOCOME; 829 } 830 } else { 831 /* 832 * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 833 */ 834 SOCKBUF_LOCK(&so->so_snd); 835 if (sbspace(&so->so_snd) < -512) { 836 SOCKBUF_UNLOCK(&so->so_snd); 837 m_freem(m); 838 error = ENOBUFS; 839 goto out; 840 } 841 /* 842 * According to RFC961 (Assigned Protocols), 843 * the urgent pointer points to the last octet 844 * of urgent data. We continue, however, 845 * to consider it to indicate the first octet 846 * of data past the urgent section. 847 * Otherwise, snd_up should be one lower. 848 */ 849 sbappendstream_locked(&so->so_snd, m); 850 SOCKBUF_UNLOCK(&so->so_snd); 851 if (nam && tp->t_state < TCPS_SYN_SENT) { 852 /* 853 * Do implied connect if not yet connected, 854 * initialize window to default value, and 855 * initialize maxseg/maxopd using peer's cached 856 * MSS. 857 */ 858 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 859#ifdef INET6 860 if (isipv6) 861 error = tcp6_connect(tp, nam, td); 862 else 863#endif /* INET6 */ 864 error = tcp_connect(tp, nam, td); 865 if (error) 866 goto out; 867 tp->snd_wnd = TTCP_CLIENT_SND_WND; 868 tcp_mss(tp, -1); 869 INP_INFO_WUNLOCK(&V_tcbinfo); 870 headlocked = 0; 871 } else if (nam) { 872 INP_INFO_WUNLOCK(&V_tcbinfo); 873 headlocked = 0; 874 } 875 tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 876 tp->t_flags |= TF_FORCEDATA; 877 error = tcp_output_send(tp); 878 tp->t_flags &= ~TF_FORCEDATA; 879 } 880out: 881 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 882 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 883 INP_WUNLOCK(inp); 884 if (headlocked) 885 INP_INFO_WUNLOCK(&V_tcbinfo); 886 return (error); 887} 888 889/* 890 * Abort the TCP. Drop the connection abruptly. 891 */ 892static void 893tcp_usr_abort(struct socket *so) 894{ 895 struct inpcb *inp; 896 struct tcpcb *tp = NULL; 897 TCPDEBUG0; 898 899 inp = sotoinpcb(so); 900 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 901 902 INP_INFO_WLOCK(&V_tcbinfo); 903 INP_WLOCK(inp); 904 KASSERT(inp->inp_socket != NULL, 905 ("tcp_usr_abort: inp_socket == NULL")); 906 907 /* 908 * If we still have full TCP state, and we're not dropped, drop. 909 */ 910 if (!(inp->inp_flags & INP_TIMEWAIT) && 911 !(inp->inp_flags & INP_DROPPED)) { 912 tp = intotcpcb(inp); 913 TCPDEBUG1(); 914 tcp_drop(tp, ECONNABORTED); 915 TCPDEBUG2(PRU_ABORT); 916 } 917 if (!(inp->inp_flags & INP_DROPPED)) { 918 SOCK_LOCK(so); 919 so->so_state |= SS_PROTOREF; 920 SOCK_UNLOCK(so); 921 inp->inp_flags |= INP_SOCKREF; 922 } 923 INP_WUNLOCK(inp); 924 INP_INFO_WUNLOCK(&V_tcbinfo); 925} 926 927/* 928 * TCP socket is closed. Start friendly disconnect. 929 */ 930static void 931tcp_usr_close(struct socket *so) 932{ 933 struct inpcb *inp; 934 struct tcpcb *tp = NULL; 935 TCPDEBUG0; 936 937 inp = sotoinpcb(so); 938 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 939 940 INP_INFO_WLOCK(&V_tcbinfo); 941 INP_WLOCK(inp); 942 KASSERT(inp->inp_socket != NULL, 943 ("tcp_usr_close: inp_socket == NULL")); 944 945 /* 946 * If we still have full TCP state, and we're not dropped, initiate 947 * a disconnect. 948 */ 949 if (!(inp->inp_flags & INP_TIMEWAIT) && 950 !(inp->inp_flags & INP_DROPPED)) { 951 tp = intotcpcb(inp); 952 TCPDEBUG1(); 953 tcp_disconnect(tp); 954 TCPDEBUG2(PRU_CLOSE); 955 } 956 if (!(inp->inp_flags & INP_DROPPED)) { 957 SOCK_LOCK(so); 958 so->so_state |= SS_PROTOREF; 959 SOCK_UNLOCK(so); 960 inp->inp_flags |= INP_SOCKREF; 961 } 962 INP_WUNLOCK(inp); 963 INP_INFO_WUNLOCK(&V_tcbinfo); 964} 965 966/* 967 * Receive out-of-band data. 968 */ 969static int 970tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 971{ 972 int error = 0; 973 struct inpcb *inp; 974 struct tcpcb *tp = NULL; 975 976 TCPDEBUG0; 977 inp = sotoinpcb(so); 978 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 979 INP_WLOCK(inp); 980 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 981 error = ECONNRESET; 982 goto out; 983 } 984 tp = intotcpcb(inp); 985 TCPDEBUG1(); 986 if ((so->so_oobmark == 0 && 987 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 988 so->so_options & SO_OOBINLINE || 989 tp->t_oobflags & TCPOOB_HADDATA) { 990 error = EINVAL; 991 goto out; 992 } 993 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 994 error = EWOULDBLOCK; 995 goto out; 996 } 997 m->m_len = 1; 998 *mtod(m, caddr_t) = tp->t_iobc; 999 if ((flags & MSG_PEEK) == 0) 1000 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1001 1002out: 1003 TCPDEBUG2(PRU_RCVOOB); 1004 INP_WUNLOCK(inp); 1005 return (error); 1006} 1007 1008struct pr_usrreqs tcp_usrreqs = { 1009 .pru_abort = tcp_usr_abort, 1010 .pru_accept = tcp_usr_accept, 1011 .pru_attach = tcp_usr_attach, 1012 .pru_bind = tcp_usr_bind, 1013 .pru_connect = tcp_usr_connect, 1014 .pru_control = in_control, 1015 .pru_detach = tcp_usr_detach, 1016 .pru_disconnect = tcp_usr_disconnect, 1017 .pru_listen = tcp_usr_listen, 1018 .pru_peeraddr = in_getpeeraddr, 1019 .pru_rcvd = tcp_usr_rcvd, 1020 .pru_rcvoob = tcp_usr_rcvoob, 1021 .pru_send = tcp_usr_send, 1022 .pru_shutdown = tcp_usr_shutdown, 1023 .pru_sockaddr = in_getsockaddr, 1024 .pru_sosetlabel = in_pcbsosetlabel, 1025 .pru_close = tcp_usr_close, 1026}; 1027 1028#ifdef INET6 1029struct pr_usrreqs tcp6_usrreqs = { 1030 .pru_abort = tcp_usr_abort, 1031 .pru_accept = tcp6_usr_accept, 1032 .pru_attach = tcp_usr_attach, 1033 .pru_bind = tcp6_usr_bind, 1034 .pru_connect = tcp6_usr_connect, 1035 .pru_control = in6_control, 1036 .pru_detach = tcp_usr_detach, 1037 .pru_disconnect = tcp_usr_disconnect, 1038 .pru_listen = tcp6_usr_listen, 1039 .pru_peeraddr = in6_mapped_peeraddr, 1040 .pru_rcvd = tcp_usr_rcvd, 1041 .pru_rcvoob = tcp_usr_rcvoob, 1042 .pru_send = tcp_usr_send, 1043 .pru_shutdown = tcp_usr_shutdown, 1044 .pru_sockaddr = in6_mapped_sockaddr, 1045 .pru_sosetlabel = in_pcbsosetlabel, 1046 .pru_close = tcp_usr_close, 1047}; 1048#endif /* INET6 */ 1049 1050/* 1051 * Common subroutine to open a TCP connection to remote host specified 1052 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 1053 * port number if needed. Call in_pcbconnect_setup to do the routing and 1054 * to choose a local host address (interface). If there is an existing 1055 * incarnation of the same connection in TIME-WAIT state and if the remote 1056 * host was sending CC options and if the connection duration was < MSL, then 1057 * truncate the previous TIME-WAIT state and proceed. 1058 * Initialize connection parameters and enter SYN-SENT state. 1059 */ 1060static int 1061tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1062{ 1063 struct inpcb *inp = tp->t_inpcb, *oinp; 1064 struct socket *so = inp->inp_socket; 1065 struct in_addr laddr; 1066 u_short lport; 1067 int error; 1068 1069 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1070 INP_WLOCK_ASSERT(inp); 1071 1072 if (inp->inp_lport == 0) { 1073 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1074 if (error) 1075 return error; 1076 } 1077 1078 /* 1079 * Cannot simply call in_pcbconnect, because there might be an 1080 * earlier incarnation of this same connection still in 1081 * TIME_WAIT state, creating an ADDRINUSE error. 1082 */ 1083 laddr = inp->inp_laddr; 1084 lport = inp->inp_lport; 1085 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1086 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 1087 if (error && oinp == NULL) 1088 return error; 1089 if (oinp) 1090 return EADDRINUSE; 1091 inp->inp_laddr = laddr; 1092 in_pcbrehash(inp); 1093 1094 /* 1095 * Compute window scaling to request: 1096 * Scale to fit into sweet spot. See tcp_syncache.c. 1097 * XXX: This should move to tcp_output(). 1098 */ 1099 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1100 (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1101 tp->request_r_scale++; 1102 1103 soisconnecting(so); 1104 TCPSTAT_INC(tcps_connattempt); 1105 tp->t_state = TCPS_SYN_SENT; 1106 tcp_timer_activate(tp, TT_KEEP, tcp_keepinit); 1107 tp->iss = tcp_new_isn(tp); 1108 tcp_sendseqinit(tp); 1109 1110 return 0; 1111} 1112 1113#ifdef INET6 1114static int 1115tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1116{ 1117 struct inpcb *inp = tp->t_inpcb, *oinp; 1118 struct socket *so = inp->inp_socket; 1119 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 1120 struct in6_addr addr6; 1121 int error; 1122 1123 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1124 INP_WLOCK_ASSERT(inp); 1125 1126 if (inp->inp_lport == 0) { 1127 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1128 if (error) 1129 return error; 1130 } 1131 1132 /* 1133 * Cannot simply call in_pcbconnect, because there might be an 1134 * earlier incarnation of this same connection still in 1135 * TIME_WAIT state, creating an ADDRINUSE error. 1136 * in6_pcbladdr() also handles scope zone IDs. 1137 */ 1138 error = in6_pcbladdr(inp, nam, &addr6); 1139 if (error) 1140 return error; 1141 oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 1142 &sin6->sin6_addr, sin6->sin6_port, 1143 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 1144 ? &addr6 1145 : &inp->in6p_laddr, 1146 inp->inp_lport, 0, NULL); 1147 if (oinp) 1148 return EADDRINUSE; 1149 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1150 inp->in6p_laddr = addr6; 1151 inp->in6p_faddr = sin6->sin6_addr; 1152 inp->inp_fport = sin6->sin6_port; 1153 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 1154 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 1155 if (inp->inp_flags & IN6P_AUTOFLOWLABEL) 1156 inp->inp_flow |= 1157 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 1158 in_pcbrehash(inp); 1159 1160 /* Compute window scaling to request. */ 1161 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1162 (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1163 tp->request_r_scale++; 1164 1165 soisconnecting(so); 1166 TCPSTAT_INC(tcps_connattempt); 1167 tp->t_state = TCPS_SYN_SENT; 1168 tcp_timer_activate(tp, TT_KEEP, tcp_keepinit); 1169 tp->iss = tcp_new_isn(tp); 1170 tcp_sendseqinit(tp); 1171 1172 return 0; 1173} 1174#endif /* INET6 */ 1175 1176/* 1177 * Export TCP internal state information via a struct tcp_info, based on the 1178 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1179 * (TCP state machine, etc). We export all information using FreeBSD-native 1180 * constants -- for example, the numeric values for tcpi_state will differ 1181 * from Linux. 1182 */ 1183static void 1184tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1185{ 1186 1187 INP_WLOCK_ASSERT(tp->t_inpcb); 1188 bzero(ti, sizeof(*ti)); 1189 1190 ti->tcpi_state = tp->t_state; 1191 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1192 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 1193 if (tp->t_flags & TF_SACK_PERMIT) 1194 ti->tcpi_options |= TCPI_OPT_SACK; 1195 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1196 ti->tcpi_options |= TCPI_OPT_WSCALE; 1197 ti->tcpi_snd_wscale = tp->snd_scale; 1198 ti->tcpi_rcv_wscale = tp->rcv_scale; 1199 } 1200 1201 ti->tcpi_rto = tp->t_rxtcur * tick; 1202 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick; 1203 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 1204 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 1205 1206 ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1207 ti->tcpi_snd_cwnd = tp->snd_cwnd; 1208 1209 /* 1210 * FreeBSD-specific extension fields for tcp_info. 1211 */ 1212 ti->tcpi_rcv_space = tp->rcv_wnd; 1213 ti->tcpi_rcv_nxt = tp->rcv_nxt; 1214 ti->tcpi_snd_wnd = tp->snd_wnd; 1215 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1216 ti->tcpi_snd_nxt = tp->snd_nxt; 1217 ti->tcpi_snd_mss = tp->t_maxseg; 1218 ti->tcpi_rcv_mss = tp->t_maxseg; 1219 if (tp->t_flags & TF_TOE) 1220 ti->tcpi_options |= TCPI_OPT_TOE; 1221 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1222 ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1223 ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1224} 1225 1226/* 1227 * tcp_ctloutput() must drop the inpcb lock before performing copyin on 1228 * socket option arguments. When it re-acquires the lock after the copy, it 1229 * has to revalidate that the connection is still valid for the socket 1230 * option. 1231 */ 1232#define INP_WLOCK_RECHECK(inp) do { \ 1233 INP_WLOCK(inp); \ 1234 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 1235 INP_WUNLOCK(inp); \ 1236 return (ECONNRESET); \ 1237 } \ 1238 tp = intotcpcb(inp); \ 1239} while(0) 1240 1241int 1242tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1243{ 1244 int error, opt, optval; 1245 struct inpcb *inp; 1246 struct tcpcb *tp; 1247 struct tcp_info ti; 1248 char buf[TCP_CA_NAME_MAX]; 1249 struct cc_algo *algo; 1250 1251 error = 0; 1252 inp = sotoinpcb(so); 1253 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1254 INP_WLOCK(inp); 1255 if (sopt->sopt_level != IPPROTO_TCP) { 1256#ifdef INET6 1257 if (inp->inp_vflag & INP_IPV6PROTO) { 1258 INP_WUNLOCK(inp); 1259 error = ip6_ctloutput(so, sopt); 1260 } else { 1261#endif /* INET6 */ 1262 INP_WUNLOCK(inp); 1263 error = ip_ctloutput(so, sopt); 1264#ifdef INET6 1265 } 1266#endif 1267 return (error); 1268 } 1269 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1270 INP_WUNLOCK(inp); 1271 return (ECONNRESET); 1272 } 1273 1274 switch (sopt->sopt_dir) { 1275 case SOPT_SET: 1276 switch (sopt->sopt_name) { 1277#ifdef TCP_SIGNATURE 1278 case TCP_MD5SIG: 1279 INP_WUNLOCK(inp); 1280 error = sooptcopyin(sopt, &optval, sizeof optval, 1281 sizeof optval); 1282 if (error) 1283 return (error); 1284 1285 INP_WLOCK_RECHECK(inp); 1286 if (optval > 0) 1287 tp->t_flags |= TF_SIGNATURE; 1288 else 1289 tp->t_flags &= ~TF_SIGNATURE; 1290 INP_WUNLOCK(inp); 1291 break; 1292#endif /* TCP_SIGNATURE */ 1293 case TCP_NODELAY: 1294 case TCP_NOOPT: 1295 INP_WUNLOCK(inp); 1296 error = sooptcopyin(sopt, &optval, sizeof optval, 1297 sizeof optval); 1298 if (error) 1299 return (error); 1300 1301 INP_WLOCK_RECHECK(inp); 1302 switch (sopt->sopt_name) { 1303 case TCP_NODELAY: 1304 opt = TF_NODELAY; 1305 break; 1306 case TCP_NOOPT: 1307 opt = TF_NOOPT; 1308 break; 1309 default: 1310 opt = 0; /* dead code to fool gcc */ 1311 break; 1312 } 1313 1314 if (optval) 1315 tp->t_flags |= opt; 1316 else 1317 tp->t_flags &= ~opt; 1318 INP_WUNLOCK(inp); 1319 break; 1320 1321 case TCP_NOPUSH: 1322 INP_WUNLOCK(inp); 1323 error = sooptcopyin(sopt, &optval, sizeof optval, 1324 sizeof optval); 1325 if (error) 1326 return (error); 1327 1328 INP_WLOCK_RECHECK(inp); 1329 if (optval) 1330 tp->t_flags |= TF_NOPUSH; 1331 else if (tp->t_flags & TF_NOPUSH) { 1332 tp->t_flags &= ~TF_NOPUSH; 1333 if (TCPS_HAVEESTABLISHED(tp->t_state)) 1334 error = tcp_output(tp); 1335 } 1336 INP_WUNLOCK(inp); 1337 break; 1338 1339 case TCP_MAXSEG: 1340 INP_WUNLOCK(inp); 1341 error = sooptcopyin(sopt, &optval, sizeof optval, 1342 sizeof optval); 1343 if (error) 1344 return (error); 1345 1346 INP_WLOCK_RECHECK(inp); 1347 if (optval > 0 && optval <= tp->t_maxseg && 1348 optval + 40 >= V_tcp_minmss) 1349 tp->t_maxseg = optval; 1350 else 1351 error = EINVAL; 1352 INP_WUNLOCK(inp); 1353 break; 1354 1355 case TCP_INFO: 1356 INP_WUNLOCK(inp); 1357 error = EINVAL; 1358 break; 1359 1360 case TCP_CONGESTION: 1361 INP_WUNLOCK(inp); 1362 bzero(buf, sizeof(buf)); 1363 error = sooptcopyin(sopt, &buf, sizeof(buf), 1); 1364 if (error) 1365 break; 1366 INP_WLOCK_RECHECK(inp); 1367 /* 1368 * Return EINVAL if we can't find the requested cc algo. 1369 */ 1370 error = EINVAL; 1371 CC_LIST_RLOCK(); 1372 STAILQ_FOREACH(algo, &cc_list, entries) { 1373 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX) 1374 == 0) { 1375 /* We've found the requested algo. */ 1376 error = 0; 1377 /* 1378 * We hold a write lock over the tcb 1379 * so it's safe to do these things 1380 * without ordering concerns. 1381 */ 1382 if (CC_ALGO(tp)->cb_destroy != NULL) 1383 CC_ALGO(tp)->cb_destroy(tp->ccv); 1384 CC_ALGO(tp) = algo; 1385 /* 1386 * If something goes pear shaped 1387 * initialising the new algo, 1388 * fall back to newreno (which 1389 * does not require initialisation). 1390 */ 1391 if (algo->cb_init != NULL) 1392 if (algo->cb_init(tp->ccv) > 0) { 1393 CC_ALGO(tp) = &newreno_cc_algo; 1394 /* 1395 * The only reason init 1396 * should fail is 1397 * because of malloc. 1398 */ 1399 error = ENOMEM; 1400 } 1401 break; /* Break the STAILQ_FOREACH. */ 1402 } 1403 } 1404 CC_LIST_RUNLOCK(); 1405 INP_WUNLOCK(inp); 1406 break; 1407 1408 default: 1409 INP_WUNLOCK(inp); 1410 error = ENOPROTOOPT; 1411 break; 1412 } 1413 break; 1414 1415 case SOPT_GET: 1416 tp = intotcpcb(inp); 1417 switch (sopt->sopt_name) { 1418#ifdef TCP_SIGNATURE 1419 case TCP_MD5SIG: 1420 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 1421 INP_WUNLOCK(inp); 1422 error = sooptcopyout(sopt, &optval, sizeof optval); 1423 break; 1424#endif 1425 1426 case TCP_NODELAY: 1427 optval = tp->t_flags & TF_NODELAY; 1428 INP_WUNLOCK(inp); 1429 error = sooptcopyout(sopt, &optval, sizeof optval); 1430 break; 1431 case TCP_MAXSEG: 1432 optval = tp->t_maxseg; 1433 INP_WUNLOCK(inp); 1434 error = sooptcopyout(sopt, &optval, sizeof optval); 1435 break; 1436 case TCP_NOOPT: 1437 optval = tp->t_flags & TF_NOOPT; 1438 INP_WUNLOCK(inp); 1439 error = sooptcopyout(sopt, &optval, sizeof optval); 1440 break; 1441 case TCP_NOPUSH: 1442 optval = tp->t_flags & TF_NOPUSH; 1443 INP_WUNLOCK(inp); 1444 error = sooptcopyout(sopt, &optval, sizeof optval); 1445 break; 1446 case TCP_INFO: 1447 tcp_fill_info(tp, &ti); 1448 INP_WUNLOCK(inp); 1449 error = sooptcopyout(sopt, &ti, sizeof ti); 1450 break; 1451 case TCP_CONGESTION: 1452 bzero(buf, sizeof(buf)); 1453 strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 1454 INP_WUNLOCK(inp); 1455 error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX); 1456 break; 1457 default: 1458 INP_WUNLOCK(inp); 1459 error = ENOPROTOOPT; 1460 break; 1461 } 1462 break; 1463 } 1464 return (error); 1465} 1466#undef INP_WLOCK_RECHECK 1467 1468/* 1469 * tcp_sendspace and tcp_recvspace are the default send and receive window 1470 * sizes, respectively. These are obsolescent (this information should 1471 * be set by the route). 1472 */ 1473u_long tcp_sendspace = 1024*32; 1474SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 1475 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 1476u_long tcp_recvspace = 1024*64; 1477SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 1478 &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1479 1480/* 1481 * Attach TCP protocol to socket, allocating 1482 * internet protocol control block, tcp control block, 1483 * bufer space, and entering LISTEN state if to accept connections. 1484 */ 1485static int 1486tcp_attach(struct socket *so) 1487{ 1488 struct tcpcb *tp; 1489 struct inpcb *inp; 1490 int error; 1491 1492 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1493 error = soreserve(so, tcp_sendspace, tcp_recvspace); 1494 if (error) 1495 return (error); 1496 } 1497 so->so_rcv.sb_flags |= SB_AUTOSIZE; 1498 so->so_snd.sb_flags |= SB_AUTOSIZE; 1499 INP_INFO_WLOCK(&V_tcbinfo); 1500 error = in_pcballoc(so, &V_tcbinfo); 1501 if (error) { 1502 INP_INFO_WUNLOCK(&V_tcbinfo); 1503 return (error); 1504 } 1505 inp = sotoinpcb(so); 1506#ifdef INET6 1507 if (inp->inp_vflag & INP_IPV6PROTO) { 1508 inp->inp_vflag |= INP_IPV6; 1509 inp->in6p_hops = -1; /* use kernel default */ 1510 } 1511 else 1512#endif 1513 inp->inp_vflag |= INP_IPV4; 1514 tp = tcp_newtcpcb(inp); 1515 if (tp == NULL) { 1516 in_pcbdetach(inp); 1517 in_pcbfree(inp); 1518 INP_INFO_WUNLOCK(&V_tcbinfo); 1519 return (ENOBUFS); 1520 } 1521 tp->t_state = TCPS_CLOSED; 1522 INP_WUNLOCK(inp); 1523 INP_INFO_WUNLOCK(&V_tcbinfo); 1524 return (0); 1525} 1526 1527/* 1528 * Initiate (or continue) disconnect. 1529 * If embryonic state, just send reset (once). 1530 * If in ``let data drain'' option and linger null, just drop. 1531 * Otherwise (hard), mark socket disconnecting and drop 1532 * current input data; switch states based on user close, and 1533 * send segment to peer (with FIN). 1534 */ 1535static void 1536tcp_disconnect(struct tcpcb *tp) 1537{ 1538 struct inpcb *inp = tp->t_inpcb; 1539 struct socket *so = inp->inp_socket; 1540 1541 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1542 INP_WLOCK_ASSERT(inp); 1543 1544 /* 1545 * Neither tcp_close() nor tcp_drop() should return NULL, as the 1546 * socket is still open. 1547 */ 1548 if (tp->t_state < TCPS_ESTABLISHED) { 1549 tp = tcp_close(tp); 1550 KASSERT(tp != NULL, 1551 ("tcp_disconnect: tcp_close() returned NULL")); 1552 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 1553 tp = tcp_drop(tp, 0); 1554 KASSERT(tp != NULL, 1555 ("tcp_disconnect: tcp_drop() returned NULL")); 1556 } else { 1557 soisdisconnecting(so); 1558 sbflush(&so->so_rcv); 1559 tcp_usrclosed(tp); 1560 if (!(inp->inp_flags & INP_DROPPED)) 1561 tcp_output_disconnect(tp); 1562 } 1563} 1564 1565/* 1566 * User issued close, and wish to trail through shutdown states: 1567 * if never received SYN, just forget it. If got a SYN from peer, 1568 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1569 * If already got a FIN from peer, then almost done; go to LAST_ACK 1570 * state. In all other cases, have already sent FIN to peer (e.g. 1571 * after PRU_SHUTDOWN), and just have to play tedious game waiting 1572 * for peer to send FIN or not respond to keep-alives, etc. 1573 * We can let the user exit from the close as soon as the FIN is acked. 1574 */ 1575static void 1576tcp_usrclosed(struct tcpcb *tp) 1577{ 1578 1579 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1580 INP_WLOCK_ASSERT(tp->t_inpcb); 1581 1582 switch (tp->t_state) { 1583 case TCPS_LISTEN: 1584 tcp_offload_listen_close(tp); 1585 /* FALLTHROUGH */ 1586 case TCPS_CLOSED: 1587 tp->t_state = TCPS_CLOSED; 1588 tp = tcp_close(tp); 1589 /* 1590 * tcp_close() should never return NULL here as the socket is 1591 * still open. 1592 */ 1593 KASSERT(tp != NULL, 1594 ("tcp_usrclosed: tcp_close() returned NULL")); 1595 break; 1596 1597 case TCPS_SYN_SENT: 1598 case TCPS_SYN_RECEIVED: 1599 tp->t_flags |= TF_NEEDFIN; 1600 break; 1601 1602 case TCPS_ESTABLISHED: 1603 tp->t_state = TCPS_FIN_WAIT_1; 1604 break; 1605 1606 case TCPS_CLOSE_WAIT: 1607 tp->t_state = TCPS_LAST_ACK; 1608 break; 1609 } 1610 if (tp->t_state >= TCPS_FIN_WAIT_2) { 1611 soisdisconnected(tp->t_inpcb->inp_socket); 1612 /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 1613 if (tp->t_state == TCPS_FIN_WAIT_2) { 1614 int timeout; 1615 1616 timeout = (tcp_fast_finwait2_recycle) ? 1617 tcp_finwait2_timeout : tcp_maxidle; 1618 tcp_timer_activate(tp, TT_2MSL, timeout); 1619 } 1620 } 1621} 1622 1623#ifdef DDB 1624static void 1625db_print_indent(int indent) 1626{ 1627 int i; 1628 1629 for (i = 0; i < indent; i++) 1630 db_printf(" "); 1631} 1632 1633static void 1634db_print_tstate(int t_state) 1635{ 1636 1637 switch (t_state) { 1638 case TCPS_CLOSED: 1639 db_printf("TCPS_CLOSED"); 1640 return; 1641 1642 case TCPS_LISTEN: 1643 db_printf("TCPS_LISTEN"); 1644 return; 1645 1646 case TCPS_SYN_SENT: 1647 db_printf("TCPS_SYN_SENT"); 1648 return; 1649 1650 case TCPS_SYN_RECEIVED: 1651 db_printf("TCPS_SYN_RECEIVED"); 1652 return; 1653 1654 case TCPS_ESTABLISHED: 1655 db_printf("TCPS_ESTABLISHED"); 1656 return; 1657 1658 case TCPS_CLOSE_WAIT: 1659 db_printf("TCPS_CLOSE_WAIT"); 1660 return; 1661 1662 case TCPS_FIN_WAIT_1: 1663 db_printf("TCPS_FIN_WAIT_1"); 1664 return; 1665 1666 case TCPS_CLOSING: 1667 db_printf("TCPS_CLOSING"); 1668 return; 1669 1670 case TCPS_LAST_ACK: 1671 db_printf("TCPS_LAST_ACK"); 1672 return; 1673 1674 case TCPS_FIN_WAIT_2: 1675 db_printf("TCPS_FIN_WAIT_2"); 1676 return; 1677 1678 case TCPS_TIME_WAIT: 1679 db_printf("TCPS_TIME_WAIT"); 1680 return; 1681 1682 default: 1683 db_printf("unknown"); 1684 return; 1685 } 1686} 1687 1688static void 1689db_print_tflags(u_int t_flags) 1690{ 1691 int comma; 1692 1693 comma = 0; 1694 if (t_flags & TF_ACKNOW) { 1695 db_printf("%sTF_ACKNOW", comma ? ", " : ""); 1696 comma = 1; 1697 } 1698 if (t_flags & TF_DELACK) { 1699 db_printf("%sTF_DELACK", comma ? ", " : ""); 1700 comma = 1; 1701 } 1702 if (t_flags & TF_NODELAY) { 1703 db_printf("%sTF_NODELAY", comma ? ", " : ""); 1704 comma = 1; 1705 } 1706 if (t_flags & TF_NOOPT) { 1707 db_printf("%sTF_NOOPT", comma ? ", " : ""); 1708 comma = 1; 1709 } 1710 if (t_flags & TF_SENTFIN) { 1711 db_printf("%sTF_SENTFIN", comma ? ", " : ""); 1712 comma = 1; 1713 } 1714 if (t_flags & TF_REQ_SCALE) { 1715 db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 1716 comma = 1; 1717 } 1718 if (t_flags & TF_RCVD_SCALE) { 1719 db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 1720 comma = 1; 1721 } 1722 if (t_flags & TF_REQ_TSTMP) { 1723 db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 1724 comma = 1; 1725 } 1726 if (t_flags & TF_RCVD_TSTMP) { 1727 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 1728 comma = 1; 1729 } 1730 if (t_flags & TF_SACK_PERMIT) { 1731 db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 1732 comma = 1; 1733 } 1734 if (t_flags & TF_NEEDSYN) { 1735 db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 1736 comma = 1; 1737 } 1738 if (t_flags & TF_NEEDFIN) { 1739 db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 1740 comma = 1; 1741 } 1742 if (t_flags & TF_NOPUSH) { 1743 db_printf("%sTF_NOPUSH", comma ? ", " : ""); 1744 comma = 1; 1745 } 1746 if (t_flags & TF_MORETOCOME) { 1747 db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 1748 comma = 1; 1749 } 1750 if (t_flags & TF_LQ_OVERFLOW) { 1751 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 1752 comma = 1; 1753 } 1754 if (t_flags & TF_LASTIDLE) { 1755 db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 1756 comma = 1; 1757 } 1758 if (t_flags & TF_RXWIN0SENT) { 1759 db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 1760 comma = 1; 1761 } 1762 if (t_flags & TF_FASTRECOVERY) { 1763 db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 1764 comma = 1; 1765 } 1766 if (t_flags & TF_CONGRECOVERY) { 1767 db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 1768 comma = 1; 1769 } 1770 if (t_flags & TF_WASFRECOVERY) { 1771 db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 1772 comma = 1; 1773 } 1774 if (t_flags & TF_SIGNATURE) { 1775 db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 1776 comma = 1; 1777 } 1778 if (t_flags & TF_FORCEDATA) { 1779 db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 1780 comma = 1; 1781 } 1782 if (t_flags & TF_TSO) { 1783 db_printf("%sTF_TSO", comma ? ", " : ""); 1784 comma = 1; 1785 } 1786 if (t_flags & TF_ECN_PERMIT) { 1787 db_printf("%sTF_ECN_PERMIT", comma ? ", " : ""); 1788 comma = 1; 1789 } 1790} 1791 1792static void 1793db_print_toobflags(char t_oobflags) 1794{ 1795 int comma; 1796 1797 comma = 0; 1798 if (t_oobflags & TCPOOB_HAVEDATA) { 1799 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 1800 comma = 1; 1801 } 1802 if (t_oobflags & TCPOOB_HADDATA) { 1803 db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 1804 comma = 1; 1805 } 1806} 1807 1808static void 1809db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 1810{ 1811 1812 db_print_indent(indent); 1813 db_printf("%s at %p\n", name, tp); 1814 1815 indent += 2; 1816 1817 db_print_indent(indent); 1818 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 1819 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 1820 1821 db_print_indent(indent); 1822 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 1823 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 1824 1825 db_print_indent(indent); 1826 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 1827 &tp->t_timers->tt_delack, tp->t_inpcb); 1828 1829 db_print_indent(indent); 1830 db_printf("t_state: %d (", tp->t_state); 1831 db_print_tstate(tp->t_state); 1832 db_printf(")\n"); 1833 1834 db_print_indent(indent); 1835 db_printf("t_flags: 0x%x (", tp->t_flags); 1836 db_print_tflags(tp->t_flags); 1837 db_printf(")\n"); 1838 1839 db_print_indent(indent); 1840 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 1841 tp->snd_una, tp->snd_max, tp->snd_nxt); 1842 1843 db_print_indent(indent); 1844 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 1845 tp->snd_up, tp->snd_wl1, tp->snd_wl2); 1846 1847 db_print_indent(indent); 1848 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 1849 tp->iss, tp->irs, tp->rcv_nxt); 1850 1851 db_print_indent(indent); 1852 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n", 1853 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 1854 1855 db_print_indent(indent); 1856 db_printf("snd_wnd: %lu snd_cwnd: %lu\n", 1857 tp->snd_wnd, tp->snd_cwnd); 1858 1859 db_print_indent(indent); 1860 db_printf("snd_ssthresh: %lu snd_recover: " 1861 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 1862 1863 db_print_indent(indent); 1864 db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n", 1865 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime); 1866 1867 db_print_indent(indent); 1868 db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 1869 tp->t_rtttime, tp->t_rtseq); 1870 1871 db_print_indent(indent); 1872 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 1873 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 1874 1875 db_print_indent(indent); 1876 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 1877 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 1878 tp->t_rttbest); 1879 1880 db_print_indent(indent); 1881 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n", 1882 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 1883 1884 db_print_indent(indent); 1885 db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 1886 db_print_toobflags(tp->t_oobflags); 1887 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 1888 1889 db_print_indent(indent); 1890 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 1891 tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 1892 1893 db_print_indent(indent); 1894 db_printf("ts_recent: %u ts_recent_age: %u\n", 1895 tp->ts_recent, tp->ts_recent_age); 1896 1897 db_print_indent(indent); 1898 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 1899 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 1900 1901 db_print_indent(indent); 1902 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x " 1903 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 1904 tp->snd_recover_prev, tp->t_badrxtwin); 1905 1906 db_print_indent(indent); 1907 db_printf("snd_numholes: %d snd_holes first: %p\n", 1908 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 1909 1910 db_print_indent(indent); 1911 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: " 1912 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata); 1913 1914 /* Skip sackblks, sackhint. */ 1915 1916 db_print_indent(indent); 1917 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 1918 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 1919} 1920 1921DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 1922{ 1923 struct tcpcb *tp; 1924 1925 if (!have_addr) { 1926 db_printf("usage: show tcpcb <addr>\n"); 1927 return; 1928 } 1929 tp = (struct tcpcb *)addr; 1930 1931 db_print_tcpcb(tp, "tcpcb", 0); 1932} 1933#endif 1934