/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*-
* Copyright (c) 1997, 1998, 2005, 2006 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
* Facility, NASA Ames Research Center.
* This code is derived from software contributed to The NetBSD Foundation
* by Charles M. Hannum.
* This code is derived from software contributed to The NetBSD Foundation
* by Rui Paulo.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1982, 1986, 1988, 1993, 1995
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)tcp_usrreq.c 8.5 (Berkeley) 6/21/95
*/
/*
* TCP protocol interface to socket abstraction.
*/
/*
* Export TCP internal state information via a struct tcp_info, based on the
* Linux 2.6 API. Not ABI compatible as our constants are mapped differently
* (TCP state machine, etc). We export all information using FreeBSD-native
* constants -- for example, the numeric values for tcpi_state will differ
* from Linux.
*/
static void
tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
{
ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
/* Linux API wants these in # of segments, apparently */
ti->tcpi_snd_cwnd = tp->snd_cwnd / tp->t_segsz;
ti->tcpi_snd_wnd = tp->snd_wnd / tp->t_segsz;
/*
* FreeBSD-specific extension fields for tcp_info.
*/
ti->tcpi_rcv_space = tp->rcv_wnd;
ti->tcpi_rcv_nxt = tp->rcv_nxt;
ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
ti->tcpi_snd_nxt = tp->snd_nxt;
ti->tcpi_snd_mss = tp->t_segsz;
ti->tcpi_rcv_mss = tp->t_segsz;
#ifdef TF_TOE
if (tp->t_flags & TF_TOE)
ti->tcpi_options |= TCPI_OPT_TOE;
#endif
/* From the redundant department of redundancies... */
ti->__tcpi_retransmits = ti->__tcpi_retrans =
ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
s = splsoftnet();
inp = sotoinpcb(so);
if (inp == NULL) {
splx(s);
return ECONNRESET;
}
if (level != IPPROTO_TCP) {
switch (family) {
case PF_INET:
error = ip_ctloutput(op, so, sopt);
break;
#ifdef INET6
case PF_INET6:
error = ip6_ctloutput(op, so, sopt);
break;
#endif
}
splx(s);
return error;
}
tp = intotcpcb(inp);
switch (op) {
case PRCO_SETOPT:
switch (optname) {
#ifdef TCP_SIGNATURE
case TCP_MD5SIG:
error = sockopt_getint(sopt, &optval);
if (error)
break;
if (optval > 0)
tp->t_flags |= TF_SIGNATURE;
else
tp->t_flags &= ~TF_SIGNATURE;
break;
#endif /* TCP_SIGNATURE */
case TCP_NODELAY:
error = sockopt_getint(sopt, &optval);
if (error)
break;
if (optval)
tp->t_flags |= TF_NODELAY;
else
tp->t_flags &= ~TF_NODELAY;
break;
case TCP_MAXSEG:
error = sockopt_getint(sopt, &optval);
if (error)
break;
if (optval > 0 && optval <= tp->t_peermss)
tp->t_peermss = optval; /* limit on send size */
else
error = EINVAL;
break;
#ifdef notyet
case TCP_CONGCTL:
/* XXX string overflow XXX */
error = tcp_congctl_select(tp, sopt->sopt_data);
break;
#endif
case TCP_KEEPIDLE:
error = sockopt_get(sopt, &ui, sizeof(ui));
if (error)
break;
if (ui > 0 && ui <= TCP_TIMER_MAXTICKS) {
tp->t_keepidle = ui;
change_keepalive(so, tp);
} else
error = EINVAL;
break;
case TCP_KEEPINTVL:
error = sockopt_get(sopt, &ui, sizeof(ui));
if (error)
break;
if (ui > 0 && ui <= TCP_TIMER_MAXTICKS) {
tp->t_keepintvl = ui;
change_keepalive(so, tp);
} else
error = EINVAL;
break;
case TCP_KEEPCNT:
error = sockopt_get(sopt, &ui, sizeof(ui));
if (error)
break;
if (ui > 0 && ui <= TCP_TIMER_MAXTICKS) {
tp->t_keepcnt = ui;
change_keepalive(so, tp);
} else
error = EINVAL;
break;
case TCP_KEEPINIT:
error = sockopt_get(sopt, &ui, sizeof(ui));
if (error)
break;
if (ui > 0 && ui <= TCP_TIMER_MAXTICKS) {
tp->t_keepinit = ui;
change_keepalive(so, tp);
} else
error = EINVAL;
break;
default:
error = ENOPROTOOPT;
break;
}
break;
case PRCO_GETOPT:
switch (optname) {
#ifdef TCP_SIGNATURE
case TCP_MD5SIG:
optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
goto setval;
#endif
case TCP_NODELAY:
optval = tp->t_flags & TF_NODELAY;
goto setval;
case TCP_MAXSEG:
optval = tp->t_peermss;
goto setval;
case TCP_INFO:
tcp_fill_info(tp, &ti);
error = sockopt_set(sopt, &ti, sizeof ti);
break;
#ifdef notyet
case TCP_CONGCTL:
break;
#endif
case TCP_KEEPIDLE:
optval = tp->t_keepidle;
goto setval;
case TCP_KEEPINTVL:
optval = tp->t_keepintvl;
goto setval;
case TCP_KEEPCNT:
optval = tp->t_keepcnt;
goto setval;
case TCP_KEEPINIT:
optval = tp->t_keepinit;
setval: error = sockopt_set(sopt, &optval, sizeof(optval));
break;
default:
error = ENOPROTOOPT;
break;
}
break;
}
splx(s);
return error;
}
#ifndef TCP_SENDSPACE
#define TCP_SENDSPACE 1024*32
#endif
int tcp_sendspace = TCP_SENDSPACE;
#ifndef TCP_RECVSPACE
#define TCP_RECVSPACE 1024*32
#endif
int tcp_recvspace = TCP_RECVSPACE;
/*
* tcp_attach: attach TCP protocol to socket, allocating internet protocol
* control block, TCP control block, buffer space and entering LISTEN state
* if to accept connections.
*/
static int
tcp_attach(struct socket *so, int proto)
{
struct tcpcb *tp;
struct inpcb *inp;
int s, error, family;
/* Assign the lock (must happen even if we will error out). */
s = splsoftnet();
sosetlock(so);
KASSERT(solocked(so));
KASSERT(sotoinpcb(so) == NULL);
/*
* Accept a connection. Essentially all the work is
* done at higher levels; just return the address
* of the peer, storing through addr.
*/
s = splsoftnet();
if (inp->inp_af == AF_INET) {
inpcb_fetch_peeraddr(inp, (struct sockaddr_in *)nam);
}
#ifdef INET6
else if (inp->inp_af == AF_INET6) {
in6pcb_fetch_peeraddr(inp, (struct sockaddr_in6 *)nam);
}
#endif
tcp_debug_trace(so, tp, ostate, PRU_ACCEPT);
splx(s);
/*
* Initiate connection to peer.
* Create a template for use in transmissions on this connection.
* Enter SYN_SENT state, and mark socket as connecting.
* Start keep-alive timer, and seed output sequence space.
* Send initial segment on connection.
*/
s = splsoftnet();
if (inp->inp_af == AF_INET) {
if (inp->inp_lport == 0) {
error = inpcb_bind(inp, NULL, l);
if (error)
goto release;
}
error = inpcb_connect(inp, (struct sockaddr_in *)nam, l);
}
#ifdef INET6
if (inp->inp_af == AF_INET6) {
if (inp->inp_lport == 0) {
error = in6pcb_bind(inp, NULL, l);
if (error)
goto release;
}
error = in6pcb_connect(inp, (struct sockaddr_in6 *)nam, l);
if (!error) {
/* mapped addr case */
if (IN6_IS_ADDR_V4MAPPED(&in6p_faddr(inp)))
tp->t_family = AF_INET;
else
tp->t_family = AF_INET6;
}
}
#endif
if (error)
goto release;
tp->t_template = tcp_template(tp);
if (tp->t_template == 0) {
if (inp->inp_af == AF_INET)
inpcb_disconnect(inp);
#ifdef INET6
else if (inp->inp_af == AF_INET6)
in6pcb_disconnect(inp);
#endif
error = ENOBUFS;
goto release;
}
/*
* Compute window scaling to request.
* XXX: This should be moved to tcp_output().
*/
while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
(TCP_MAXWIN << tp->request_r_scale) < sb_max)
tp->request_r_scale++;
soisconnecting(so);
TCP_STATINC(TCP_STAT_CONNATTEMPT);
tp->t_state = TCPS_SYN_SENT;
TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit);
tp->iss = tcp_new_iss(tp);
tcp_sendseqinit(tp);
error = tcp_output(tp);
/*
* Initiate disconnect from peer.
* If connection never passed embryonic stage, just drop;
* else if don't need to let data drain, then can just drop anyways,
* else have to begin TCP shutdown process: mark socket disconnecting,
* drain unread data, state switch to reflect user close, and
* send segment (e.g. FIN) to peer. Socket will be really disconnected
* when peer sends FIN and acks ours.
*
* SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
*/
s = splsoftnet();
tp = tcp_disconnect1(tp);
tcp_debug_trace(so, tp, ostate, PRU_DISCONNECT);
splx(s);
return error;
}
static int
tcp_shutdown(struct socket *so)
{
struct inpcb *inp;
struct tcpcb *tp;
int error = 0;
int ostate = 0;
int s;
ostate = tcp_debug_capture(tp, PRU_SHUTDOWN);
/*
* Mark the connection as being incapable of further output.
*/
s = splsoftnet();
socantsendmore(so);
tp = tcp_usrclosed(tp);
if (tp)
error = tcp_output(tp);
tcp_debug_trace(so, tp, ostate, PRU_SHUTDOWN);
splx(s);
return error;
}
static int
tcp_abort(struct socket *so)
{
struct inpcb *inp;
struct tcpcb *tp;
int error = 0;
int ostate = 0;
int s;
/*
* After a receive, possibly send window update to peer.
*
* soreceive() calls this function when a user receives
* ancillary data on a listening socket. We don't call
* tcp_output in such a case, since there is no header
* template for a listening socket and hence the kernel
* will panic.
*/
s = splsoftnet();
if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0)
(void) tcp_output(tp);
splx(s);
tcp_debug_trace(so, tp, ostate, PRU_RCVD);
return 0;
}
static int
tcp_recvoob(struct socket *so, struct mbuf *m, int flags)
{
struct inpcb *inp;
struct tcpcb *tp;
int ostate = 0;
int s;
/*
* Do a send by putting data in output queue and updating urgent
* marker if URG set. Possibly send more data.
*/
s = splsoftnet();
if (control && control->m_len) {
m_freem(control);
m_freem(m);
tcp_debug_trace(so, tp, ostate, PRU_SEND);
splx(s);
return EINVAL;
}
static int
tcp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
{
struct inpcb *inp = NULL;
struct tcpcb *tp = NULL;
int ostate = 0;
int error = 0;
int s;
inp = sotoinpcb(so);
if (inp == NULL) {
m_freem(m);
m_freem(control);
return EINVAL;
}
tp = intotcpcb(inp);
if (tp->t_template == NULL) {
/*
* XXX FreeBSD appears to open the connection
* automagically in this case, but the socket address
* isn't passed through here so we can't do that.
*/
m_freem(m);
m_freem(control);
return ENOTCONN;
}
ostate = tcp_debug_capture(tp, PRU_SENDOOB);
s = splsoftnet();
if (sbspace_oob(&so->so_snd) == 0) {
m_freem(m);
m_freem(control);
splx(s);
return ENOBUFS;
}
/*
* According to RFC961 (Assigned Protocols),
* the urgent pointer points to the last octet
* of urgent data. We continue, however,
* to consider it to indicate the first octet
* of data past the urgent section.
* Otherwise, snd_up should be one lower.
*/
sbappendstream(&so->so_snd, m);
tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
tp->t_force = 1;
error = tcp_output(tp);
tp->t_force = 0;
tcp_debug_trace(so, tp, ostate, PRU_SENDOOB);
splx(s);
m_freem(control);
return error;
}
static int
tcp_purgeif(struct socket *so, struct ifnet *ifp)
{
int s;
int error = 0;
/*
* Initiate (or continue) disconnect.
* If embryonic state, just send reset (once).
* If in ``let data drain'' option and linger null, just drop.
* Otherwise (hard), mark socket disconnecting and drop
* current input data; switch states based on user close, and
* send segment to peer (with FIN).
*/
struct tcpcb *
tcp_disconnect1(struct tcpcb *tp)
{
struct socket *so;
/*
* User issued close, and wish to trail through shutdown states:
* if never received SYN, just forget it. If got a SYN from peer,
* but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
* If already got a FIN from peer, then almost done; go to LAST_ACK
* state. In all other cases, have already sent FIN to peer (e.g.
* after PRU_SHUTDOWN), and just have to play tedious game waiting
* for peer to send FIN or not respond to keep-alives, etc.
* We can let the user exit from the close as soon as the FIN is acked.
*/
struct tcpcb *
tcp_usrclosed(struct tcpcb *tp)
{
switch (tp->t_state) {
case TCPS_CLOSED:
case TCPS_LISTEN:
case TCPS_SYN_SENT:
tp->t_state = TCPS_CLOSED;
tp = tcp_close(tp);
break;
case TCPS_SYN_RECEIVED:
case TCPS_ESTABLISHED:
tp->t_state = TCPS_FIN_WAIT_1;
break;
case TCPS_CLOSE_WAIT:
tp->t_state = TCPS_LAST_ACK;
break;
}
if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
struct socket *so = tp->t_inpcb->inp_socket;
if (so)
soisdisconnected(so);
/*
* If we are in FIN_WAIT_2, we arrived here because the
* application did a shutdown of the send side. Like the
* case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
* a full close, we start a timer to make sure sockets are
* not left in FIN_WAIT_2 forever.
*/
if ((tp->t_state == TCPS_FIN_WAIT_2) && (tp->t_maxidle > 0))
TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_maxidle);
else if (tp->t_state == TCPS_TIME_WAIT
&& ((tp->t_inpcb->inp_af == AF_INET
&& (tcp4_vtw_enable & 1)
&& vtw_add(AF_INET, tp))
||
(tp->t_inpcb->inp_af == AF_INET6
&& (tcp6_vtw_enable & 1)
&& vtw_add(AF_INET6, tp)))) {
tp = 0;
}
}
return tp;
}
/*
* sysctl helper routine for net.inet.ip.mssdflt. it can't be less
* than 32.
*/
static int
sysctl_net_inet_tcp_mssdflt(SYSCTLFN_ARGS)
{
int error, mssdflt;
struct sysctlnode node;
/*
* sysctl helper routine for setting port related values under
* net.inet.ip and net.inet6.ip6. does basic range checking and does
* additional checks for each type. this code has placed in
* tcp_input.c since INET and INET6 both use the same tcp code.
*
* this helper is not static so that both inet and inet6 can use it.
*/
int
sysctl_net_inet_ip_ports(SYSCTLFN_ARGS)
{
int error, tmp;
int apmin, apmax;
#ifndef IPNOPRIVPORTS
int lpmin, lpmax;
#endif /* IPNOPRIVPORTS */
struct sysctlnode node;
/*
* sysctl helper routine for the net.inet.tcp.drop and
* net.inet6.tcp6.drop nodes.
*/
#define sysctl_net_inet_tcp_drop sysctl_net_inet_tcp_ident
/*
* sysctl helper routine for the net.inet.tcp.ident and
* net.inet6.tcp6.ident nodes. contains backwards compat code for the
* old way of looking up the ident information for ipv4 which involves
* stuffing the port/addr pairs into the mib lookup.
*/
static int
sysctl_net_inet_tcp_ident(SYSCTLFN_ARGS)
{
struct sockaddr_in *si4[2];
#ifdef INET6
struct sockaddr_in6 *si6[2];
#endif
struct sockaddr_storage sa[2];
int error, pf, dodrop;
dodrop = name[-1] == TCPCTL_DROP;
if (dodrop) {
if (oldp != NULL || *oldlenp != 0)
return EINVAL;
if (newp == NULL)
return EPERM;
if (newlen < sizeof(sa))
return ENOMEM;
}
if (namelen != 4 && namelen != 0)
return EINVAL;
if (name[-2] != IPPROTO_TCP)
return EINVAL;
pf = name[-3];
/* old style lookup, ipv4 only */
if (namelen == 4) {
struct in_addr laddr, raddr;
u_int lport, rport;
/*
* sysctl helper for the inet and inet6 pcblists. handles tcp/udp and
* inet/inet6, as well as raw pcbs for each. specifically not
* declared static so that raw sockets and udp/udp6 can use it as
* well.
*/
int
sysctl_inpcblist(SYSCTLFN_ARGS)
{
const bool allowaddr = get_expose_address(curproc);
struct sockaddr_in *in;
const struct inpcb *inp;
#ifdef INET6
struct sockaddr_in6 *in6;
#endif
struct inpcbtable *pcbtbl = __UNCONST(rnode->sysctl_data);
struct tcpcb *tp;
struct kinfo_pcb pcb;
char *dp;
size_t len, needed, elem_size, out_size;
int error, elem_count, pf, proto, pf2;