2005-01-07 01:45:51 +00:00
|
|
|
/*-
|
1995-10-03 16:54:17 +00:00
|
|
|
* Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
|
1994-05-24 10:09:53 +00:00
|
|
|
* 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.
|
|
|
|
* 4. 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.
|
|
|
|
*
|
1995-10-03 16:54:17 +00:00
|
|
|
* @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
|
2007-10-07 20:44:24 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
Initial import of RFC 2385 (TCP-MD5) digest support.
This is the first of two commits; bringing in the kernel support first.
This can be enabled by compiling a kernel with options TCP_SIGNATURE
and FAST_IPSEC.
For the uninitiated, this is a TCP option which provides for a means of
authenticating TCP sessions which came into being before IPSEC. It is
still relevant today, however, as it is used by many commercial router
vendors, particularly with BGP, and as such has become a requirement for
interconnect at many major Internet points of presence.
Several parts of the TCP and IP headers, including the segment payload,
are digested with MD5, including a shared secret. The PF_KEY interface
is used to manage the secrets using security associations in the SADB.
There is a limitation here in that as there is no way to map a TCP flow
per-port back to an SPI without polluting tcpcb or using the SPD; the
code to do the latter is unstable at this time. Therefore this code only
supports per-host keying granularity.
Whilst FAST_IPSEC is mutually exclusive with KAME IPSEC (and thus IPv6),
TCP_SIGNATURE applies only to IPv4. For the vast majority of prospective
users of this feature, this will not pose any problem.
This implementation is output-only; that is, the option is honoured when
responding to a host initiating a TCP session, but no effort is made
[yet] to authenticate inbound traffic. This is, however, sufficient to
interwork with Cisco equipment.
Tested with a Cisco 2501 running IOS 12.0(27), and Quagga 0.96.4 with
local patches. Patches for tcpdump to validate TCP-MD5 sessions are also
available from me upon request.
Sponsored by: sentex.net
2004-02-11 04:26:04 +00:00
|
|
|
#include "opt_inet.h"
|
2000-01-09 19:17:30 +00:00
|
|
|
#include "opt_inet6.h"
|
1997-09-16 18:36:06 +00:00
|
|
|
#include "opt_tcpdebug.h"
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/param.h>
|
1995-11-09 20:23:09 +00:00
|
|
|
#include <sys/kernel.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/sysctl.h>
|
1996-04-04 10:46:44 +00:00
|
|
|
#include <sys/syslog.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/systm.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2004-02-24 15:27:41 +00:00
|
|
|
#include <vm/uma.h>
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <net/if.h>
|
2013-10-26 17:58:36 +00:00
|
|
|
#include <net/if_var.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <net/route.h>
|
2009-08-01 19:26:27 +00:00
|
|
|
#include <net/vnet.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <netinet/in_pcb.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <netinet/in_systm.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <netinet/in_var.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_var.h>
|
2005-11-18 20:12:40 +00:00
|
|
|
#include <netinet/ip_options.h>
|
2000-07-04 16:35:15 +00:00
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#include <netinet6/in6_pcb.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <netinet6/ip6_var.h>
|
|
|
|
#include <netinet6/nd6.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <netinet/tcp_fsm.h>
|
|
|
|
#include <netinet/tcp_seq.h>
|
|
|
|
#include <netinet/tcp_timer.h>
|
|
|
|
#include <netinet/tcp_var.h>
|
2000-01-09 19:17:30 +00:00
|
|
|
#include <netinet6/tcp6_var.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <netinet/tcpip.h>
|
2000-01-09 19:17:30 +00:00
|
|
|
|
2010-09-25 04:58:46 +00:00
|
|
|
void
|
|
|
|
tcp_reass_flush(struct tcpcb *tp)
|
|
|
|
{
|
2014-05-04 23:25:32 +00:00
|
|
|
struct mbuf *m;
|
2010-09-25 04:58:46 +00:00
|
|
|
|
|
|
|
INP_WLOCK_ASSERT(tp->t_inpcb);
|
|
|
|
|
2014-05-04 23:25:32 +00:00
|
|
|
while ((m = tp->t_segq) != NULL) {
|
|
|
|
tp->t_segq = m->m_nextpkt;
|
|
|
|
tp->t_segqlen -= m->m_pkthdr.len;
|
|
|
|
m_freem(m);
|
2010-09-25 04:58:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KASSERT((tp->t_segqlen == 0),
|
2014-05-04 23:25:32 +00:00
|
|
|
("TCP reass queue %p length is %d instead of 0 after flush.",
|
2010-09-25 04:58:46 +00:00
|
|
|
tp, tp->t_segqlen));
|
|
|
|
}
|
|
|
|
|
2014-05-04 23:25:32 +00:00
|
|
|
#define M_TCPHDR(m) ((struct tcphdr *)((m)->m_pkthdr.pkt_tcphdr))
|
|
|
|
|
2007-05-13 22:16:13 +00:00
|
|
|
int
|
2007-03-21 19:37:55 +00:00
|
|
|
tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
|
|
|
struct socket *so = tp->t_inpcb->inp_socket;
|
2014-05-04 23:25:32 +00:00
|
|
|
struct mbuf *mq, *mp;
|
|
|
|
int flags, wakeup;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2008-04-17 21:38:18 +00:00
|
|
|
INP_WLOCK_ASSERT(tp->t_inpcb);
|
2004-11-23 23:41:20 +00:00
|
|
|
|
2004-02-24 15:27:41 +00:00
|
|
|
/*
|
|
|
|
* XXX: tcp_reass() is rather inefficient with its data structures
|
2009-05-25 14:51:47 +00:00
|
|
|
* and should be rewritten (see NetBSD for optimizations).
|
2004-02-24 15:27:41 +00:00
|
|
|
*/
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
2004-11-23 23:41:20 +00:00
|
|
|
* Call with th==NULL after become established to
|
1994-05-24 10:09:53 +00:00
|
|
|
* force pre-ESTABLISHED data up to user socket.
|
|
|
|
*/
|
2004-11-23 23:41:20 +00:00
|
|
|
if (th == NULL)
|
1994-05-24 10:09:53 +00:00
|
|
|
goto present;
|
|
|
|
|
2014-05-04 23:25:32 +00:00
|
|
|
M_ASSERTPKTHDR(m);
|
|
|
|
KASSERT(*tlenp == m->m_pkthdr.len, ("%s: tlenp %u len %u", __func__,
|
|
|
|
*tlenp, m->m_pkthdr.len));
|
|
|
|
|
2004-02-24 15:27:41 +00:00
|
|
|
/*
|
2010-10-16 07:12:39 +00:00
|
|
|
* Limit the number of segments that can be queued to reduce the
|
|
|
|
* potential for mbuf exhaustion. For best performance, we want to be
|
|
|
|
* able to queue a full window's worth of segments. The size of the
|
|
|
|
* socket receive buffer determines our advertised window and grows
|
|
|
|
* automatically when socket buffer autotuning is enabled. Use it as the
|
|
|
|
* basis for our queue limit.
|
|
|
|
* Always let the missing segment through which caused this queue.
|
|
|
|
* NB: Access to the socket buffer is left intentionally unlocked as we
|
|
|
|
* can tolerate stale information here.
|
2004-02-24 15:27:41 +00:00
|
|
|
*/
|
2014-04-30 04:02:57 +00:00
|
|
|
if ((th->th_seq != tp->rcv_nxt || !TCPS_HAVEESTABLISHED(tp->t_state)) &&
|
2014-05-04 23:25:32 +00:00
|
|
|
tp->t_segqlen + m->m_pkthdr.len >= sbspace(&so->so_rcv)) {
|
|
|
|
char *s;
|
|
|
|
|
2014-05-06 00:00:07 +00:00
|
|
|
TCPSTAT_INC(tcps_rcvreassfull);
|
2005-04-10 05:21:29 +00:00
|
|
|
*tlenp = 0;
|
2014-05-04 23:25:32 +00:00
|
|
|
if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL,
|
|
|
|
NULL))) {
|
2011-10-07 16:39:03 +00:00
|
|
|
log(LOG_DEBUG, "%s; %s: queue limit reached, "
|
|
|
|
"segment dropped\n", s, __func__);
|
|
|
|
free(s, M_TCPLOG);
|
|
|
|
}
|
2014-05-05 21:33:20 +00:00
|
|
|
m_freem(m);
|
2004-02-24 15:27:41 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Find a segment which begins after this one does.
|
|
|
|
*/
|
2014-05-04 23:25:32 +00:00
|
|
|
mp = NULL;
|
|
|
|
for (mq = tp->t_segq; mq != NULL; mq = mq->m_nextpkt) {
|
|
|
|
if (SEQ_GT(M_TCPHDR(mq)->th_seq, th->th_seq))
|
1994-05-24 10:09:53 +00:00
|
|
|
break;
|
2014-05-04 23:25:32 +00:00
|
|
|
mp = mq;
|
2000-01-09 19:17:30 +00:00
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is a preceding segment, it may provide some of
|
|
|
|
* our data already. If so, drop the data from the incoming
|
|
|
|
* segment. If it provides all of our data, drop us.
|
|
|
|
*/
|
2014-05-04 23:25:32 +00:00
|
|
|
if (mp != NULL) {
|
2007-03-21 19:37:55 +00:00
|
|
|
int i;
|
2014-05-04 23:25:32 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/* conversion to int (in i) handles seq wraparound */
|
2014-05-04 23:25:32 +00:00
|
|
|
i = M_TCPHDR(mp)->th_seq + mp->m_pkthdr.len - th->th_seq;
|
1994-05-24 10:09:53 +00:00
|
|
|
if (i > 0) {
|
2000-01-09 19:17:30 +00:00
|
|
|
if (i >= *tlenp) {
|
2009-04-11 22:07:19 +00:00
|
|
|
TCPSTAT_INC(tcps_rcvduppack);
|
|
|
|
TCPSTAT_ADD(tcps_rcvdupbyte, *tlenp);
|
1994-05-24 10:09:53 +00:00
|
|
|
m_freem(m);
|
1995-02-09 23:13:27 +00:00
|
|
|
/*
|
|
|
|
* Try to present any queued data
|
|
|
|
* at the left window edge to the user.
|
|
|
|
* This is needed after the 3-WHS
|
|
|
|
* completes.
|
|
|
|
*/
|
|
|
|
goto present; /* ??? */
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
m_adj(m, i);
|
2000-01-09 19:17:30 +00:00
|
|
|
*tlenp -= i;
|
|
|
|
th->th_seq += i;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-07 21:40:34 +00:00
|
|
|
tp->t_rcvoopack++;
|
2009-04-11 22:07:19 +00:00
|
|
|
TCPSTAT_INC(tcps_rcvoopack);
|
|
|
|
TCPSTAT_ADD(tcps_rcvoobyte, *tlenp);
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* While we overlap succeeding segments trim them or,
|
|
|
|
* if they are completely covered, dequeue them.
|
|
|
|
*/
|
2014-05-04 23:25:32 +00:00
|
|
|
while (mq) {
|
|
|
|
struct mbuf *nq;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = (th->th_seq + *tlenp) - M_TCPHDR(mq)->th_seq;
|
1994-05-24 10:09:53 +00:00
|
|
|
if (i <= 0)
|
|
|
|
break;
|
2014-05-04 23:25:32 +00:00
|
|
|
if (i < mq->m_pkthdr.len) {
|
|
|
|
M_TCPHDR(mq)->th_seq += i;
|
|
|
|
m_adj(mq, i);
|
|
|
|
tp->t_segqlen -= i;
|
1994-05-24 10:09:53 +00:00
|
|
|
break;
|
|
|
|
}
|
1998-08-24 07:47:39 +00:00
|
|
|
|
2014-05-04 23:25:32 +00:00
|
|
|
nq = mq->m_nextpkt;
|
|
|
|
tp->t_segqlen -= mq->m_pkthdr.len;
|
|
|
|
m_freem(mq);
|
|
|
|
if (mp)
|
|
|
|
mp->m_nextpkt = nq;
|
|
|
|
else
|
|
|
|
tp->t_segq = nq;
|
|
|
|
mq = nq;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
2000-01-09 19:17:30 +00:00
|
|
|
/* Insert the new segment queue entry into place. */
|
2014-05-04 23:25:32 +00:00
|
|
|
if (mp) {
|
|
|
|
m->m_nextpkt = mp->m_nextpkt;
|
|
|
|
mp->m_nextpkt = m;
|
1998-08-24 07:47:39 +00:00
|
|
|
} else {
|
2014-05-04 23:25:32 +00:00
|
|
|
m->m_nextpkt = tp->t_segq;
|
|
|
|
tp->t_segq = m ;
|
1998-08-24 07:47:39 +00:00
|
|
|
}
|
2014-05-04 23:25:32 +00:00
|
|
|
m->m_pkthdr.pkt_tcphdr = th;
|
|
|
|
tp->t_segqlen += m->m_pkthdr.len;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
present:
|
|
|
|
/*
|
|
|
|
* Present data to user, advancing rcv_nxt through
|
|
|
|
* completed sequence space.
|
|
|
|
*/
|
1995-02-09 23:13:27 +00:00
|
|
|
if (!TCPS_HAVEESTABLISHED(tp->t_state))
|
1994-05-24 10:09:53 +00:00
|
|
|
return (0);
|
2014-05-04 23:25:32 +00:00
|
|
|
|
|
|
|
flags = 0;
|
|
|
|
wakeup = 0;
|
Reduce the number of unnecessary unlock-relocks on socket buffer mutexes
associated with performing a wakeup on the socket buffer:
- When performing an sbappend*() followed by a so[rw]wakeup(), explicitly
acquire the socket buffer lock and use the _locked() variants of both
calls. Note that the _locked() sowakeup() versions unlock the mutex on
return. This is done in uipc_send(), divert_packet(), mroute
socket_send(), raw_append(), tcp_reass(), tcp_input(), and udp_append().
- When the socket buffer lock is dropped before a sowakeup(), remove the
explicit unlock and use the _locked() sowakeup() variant. This is done
in soisdisconnecting(), soisdisconnected() when setting the can't send/
receive flags and dropping data, and in uipc_rcvd() which adjusting
back-pressure on the sockets.
For UNIX domain sockets running mpsafe with a contention-intensive SMP
mysql benchmark, this results in a 1.6% query rate improvement due to
reduce mutex costs.
2004-06-26 19:10:39 +00:00
|
|
|
SOCKBUF_LOCK(&so->so_rcv);
|
2014-05-04 23:25:32 +00:00
|
|
|
while ((mq = tp->t_segq) != NULL &&
|
|
|
|
M_TCPHDR(mq)->th_seq == tp->rcv_nxt) {
|
|
|
|
tp->t_segq = mq->m_nextpkt;
|
|
|
|
|
|
|
|
tp->rcv_nxt += mq->m_pkthdr.len;
|
|
|
|
tp->t_segqlen -= mq->m_pkthdr.len;
|
|
|
|
flags = M_TCPHDR(mq)->th_flags & TH_FIN;
|
|
|
|
|
2004-06-14 18:16:22 +00:00
|
|
|
if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
|
2014-05-04 23:25:32 +00:00
|
|
|
m_freem(mq);
|
|
|
|
else {
|
|
|
|
mq->m_nextpkt = NULL;
|
|
|
|
sbappendstream_locked(&so->so_rcv, mq);
|
|
|
|
wakeup = 1;
|
|
|
|
}
|
|
|
|
}
|
2000-01-09 19:17:30 +00:00
|
|
|
ND6_HINT(tp);
|
2014-05-04 23:25:32 +00:00
|
|
|
if (wakeup)
|
|
|
|
sorwakeup_locked(so);
|
|
|
|
else
|
|
|
|
SOCKBUF_UNLOCK(&so->so_rcv);
|
1994-05-24 10:09:53 +00:00
|
|
|
return (flags);
|
|
|
|
}
|