1999-12-08 18:55:39 +00:00
|
|
|
/*
|
|
|
|
* ng_pptpgre.c
|
2005-01-07 01:45:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*-
|
1999-12-08 18:55:39 +00:00
|
|
|
* Copyright (c) 1996-1999 Whistle Communications, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Subject to the following obligations and disclaimer of warranty, use and
|
|
|
|
* redistribution of this software, in source or object code forms, with or
|
|
|
|
* without modifications are expressly permitted by Whistle Communications;
|
|
|
|
* provided, however, that:
|
|
|
|
* 1. Any and all reproductions of the source or object code must include the
|
|
|
|
* copyright notice above and the following disclaimer of warranties; and
|
|
|
|
* 2. No rights are granted, in any manner or form, to use Whistle
|
|
|
|
* Communications, Inc. trademarks, including the mark "WHISTLE
|
|
|
|
* COMMUNICATIONS" on advertising, endorsements, or otherwise except as
|
|
|
|
* such appears in the above copyright notice or in the software.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
|
|
|
|
* TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
|
|
|
|
* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
|
|
|
|
* INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
|
|
|
|
* WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
|
|
|
|
* REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
|
|
|
|
* SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
|
|
|
|
* IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
|
|
|
|
* RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
|
|
|
|
* WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
|
|
* PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER 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 WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
2000-10-24 17:32:45 +00:00
|
|
|
* Author: Archie Cobbs <archie@freebsd.org>
|
1999-12-08 18:55:39 +00:00
|
|
|
* $Whistle: ng_pptpgre.c,v 1.7 1999/12/08 00:10:06 archie Exp $
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PPTP/GRE netgraph node type.
|
|
|
|
*
|
|
|
|
* This node type does the GRE encapsulation as specified for the PPTP
|
|
|
|
* protocol (RFC 2637, section 4). This includes sequencing and
|
|
|
|
* retransmission of frames, but not the actual packet delivery nor
|
|
|
|
* any of the TCP control stream protocol.
|
|
|
|
*
|
|
|
|
* The "upper" hook of this node is suitable for attaching to a "ppp"
|
|
|
|
* node link hook. The "lower" hook of this node is suitable for attaching
|
|
|
|
* to a "ksocket" node on hook "inet/raw/gre".
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/time.h>
|
2005-08-30 09:51:54 +00:00
|
|
|
#include <sys/lock.h>
|
1999-12-08 18:55:39 +00:00
|
|
|
#include <sys/malloc.h>
|
2005-08-30 09:51:54 +00:00
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/mutex.h>
|
2010-04-01 16:20:36 +00:00
|
|
|
#include <sys/endian.h>
|
1999-12-08 18:55:39 +00:00
|
|
|
#include <sys/errno.h>
|
2018-11-04 19:10:44 +00:00
|
|
|
#include <sys/sysctl.h>
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
|
|
|
|
#include <netgraph/ng_message.h>
|
|
|
|
#include <netgraph/netgraph.h>
|
|
|
|
#include <netgraph/ng_parse.h>
|
|
|
|
#include <netgraph/ng_pptpgre.h>
|
|
|
|
|
2024-05-31 16:19:54 +00:00
|
|
|
#ifdef NG_SEPARATE_MALLOC
|
|
|
|
static MALLOC_DEFINE(M_NETGRAPH_PPTP, "netgraph_pptp", "netgraph pptpgre node");
|
|
|
|
#else
|
|
|
|
#define M_NETGRAPH_PPTP M_NETGRAPH
|
|
|
|
#endif
|
|
|
|
|
1999-12-08 18:55:39 +00:00
|
|
|
/* GRE packet format, as used by PPTP */
|
|
|
|
struct greheader {
|
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
|
|
|
u_char recursion:3; /* recursion control */
|
|
|
|
u_char ssr:1; /* strict source route */
|
|
|
|
u_char hasSeq:1; /* sequence number present */
|
|
|
|
u_char hasKey:1; /* key present */
|
|
|
|
u_char hasRoute:1; /* routing present */
|
|
|
|
u_char hasSum:1; /* checksum present */
|
|
|
|
u_char vers:3; /* version */
|
|
|
|
u_char flags:4; /* flags */
|
|
|
|
u_char hasAck:1; /* acknowlege number present */
|
|
|
|
#elif BYTE_ORDER == BIG_ENDIAN
|
|
|
|
u_char hasSum:1; /* checksum present */
|
|
|
|
u_char hasRoute:1; /* routing present */
|
|
|
|
u_char hasKey:1; /* key present */
|
|
|
|
u_char hasSeq:1; /* sequence number present */
|
|
|
|
u_char ssr:1; /* strict source route */
|
|
|
|
u_char recursion:3; /* recursion control */
|
|
|
|
u_char hasAck:1; /* acknowlege number present */
|
|
|
|
u_char flags:4; /* flags */
|
|
|
|
u_char vers:3; /* version */
|
|
|
|
#else
|
|
|
|
#error BYTE_ORDER is not defined properly
|
|
|
|
#endif
|
|
|
|
u_int16_t proto; /* protocol (ethertype) */
|
|
|
|
u_int16_t length; /* payload length */
|
|
|
|
u_int16_t cid; /* call id */
|
|
|
|
u_int32_t data[0]; /* opt. seq, ack, then data */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The PPTP protocol ID used in the GRE 'proto' field */
|
|
|
|
#define PPTP_GRE_PROTO 0x880b
|
|
|
|
|
|
|
|
/* Bits that must be set a certain way in all PPTP/GRE packets */
|
|
|
|
#define PPTP_INIT_VALUE ((0x2001 << 16) | PPTP_GRE_PROTO)
|
|
|
|
#define PPTP_INIT_MASK 0xef7fffff
|
|
|
|
|
|
|
|
/* Min and max packet length */
|
|
|
|
#define PPTP_MAX_PAYLOAD (0xffff - sizeof(struct greheader) - 8)
|
|
|
|
|
|
|
|
/* All times are scaled by this (PPTP_TIME_SCALE time units = 1 sec.) */
|
2008-03-26 21:19:03 +00:00
|
|
|
#define PPTP_TIME_SCALE 1024 /* milliseconds */
|
2000-07-25 00:23:19 +00:00
|
|
|
typedef u_int64_t pptptime_t;
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* Acknowledgment timeout parameters and functions */
|
2000-06-28 19:43:34 +00:00
|
|
|
#define PPTP_XMIT_WIN 16 /* max xmit window */
|
2002-04-14 17:37:35 +00:00
|
|
|
#define PPTP_MIN_TIMEOUT (PPTP_TIME_SCALE / 83) /* 12 milliseconds */
|
2004-09-06 19:53:58 +00:00
|
|
|
#define PPTP_MAX_TIMEOUT (3 * PPTP_TIME_SCALE) /* 3 seconds */
|
1999-12-08 18:55:39 +00:00
|
|
|
|
2018-11-04 19:10:44 +00:00
|
|
|
#define PPTP_REORDER_TIMEOUT 1
|
|
|
|
|
2016-04-29 21:25:05 +00:00
|
|
|
/* When we receive a packet, we wait to see if there's an outgoing packet
|
2021-11-03 16:15:59 +00:00
|
|
|
we can piggy-back the ACK off of. These parameters determine the minimum
|
2000-07-25 18:57:20 +00:00
|
|
|
and maxmimum length of time we're willing to wait in order to do that.
|
|
|
|
These have no effect unless "enableDelayedAck" is turned on. */
|
|
|
|
#define PPTP_MIN_ACK_DELAY (PPTP_TIME_SCALE / 500) /* 2 milliseconds */
|
|
|
|
#define PPTP_MAX_ACK_DELAY (PPTP_TIME_SCALE / 2) /* 500 milliseconds */
|
|
|
|
|
2000-06-28 19:43:34 +00:00
|
|
|
/* See RFC 2637 section 4.4 */
|
2007-02-02 09:45:23 +00:00
|
|
|
#define PPTP_ACK_ALPHA(x) (((x) + 4) >> 3) /* alpha = 0.125 */
|
|
|
|
#define PPTP_ACK_BETA(x) (((x) + 2) >> 2) /* beta = 0.25 */
|
1999-12-08 18:55:39 +00:00
|
|
|
#define PPTP_ACK_CHI(x) ((x) << 2) /* chi = 4 */
|
|
|
|
#define PPTP_ACK_DELTA(x) ((x) << 1) /* delta = 2 */
|
|
|
|
|
2000-05-05 01:11:39 +00:00
|
|
|
#define PPTP_SEQ_DIFF(x,y) ((int32_t)(x) - (int32_t)(y))
|
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
#define SESSHASHSIZE 0x0020
|
|
|
|
#define SESSHASH(x) (((x) ^ ((x) >> 8)) & (SESSHASHSIZE - 1))
|
|
|
|
|
2020-02-26 14:26:36 +00:00
|
|
|
SYSCTL_NODE(_net_graph, OID_AUTO, pptpgre, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
|
|
|
|
"PPTPGRE");
|
2018-11-04 19:10:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Reorder queue maximum length. Zero disables reorder.
|
|
|
|
*
|
|
|
|
* The node may keep reorder_max queue entries per session
|
|
|
|
* if reorder is enabled, plus allocate one more for short time.
|
|
|
|
*
|
|
|
|
* Be conservative in memory consumption by default.
|
|
|
|
* Lots of sessions with large queues can overflow M_NETGRAPH zone.
|
|
|
|
*/
|
|
|
|
static int reorder_max = 1; /* reorder up to two swapped packets in a row */
|
|
|
|
SYSCTL_UINT(_net_graph_pptpgre, OID_AUTO, reorder_max, CTLFLAG_RWTUN,
|
|
|
|
&reorder_max, 0, "Reorder queue maximum length");
|
|
|
|
|
|
|
|
static int reorder_timeout = PPTP_REORDER_TIMEOUT;
|
|
|
|
SYSCTL_UINT(_net_graph_pptpgre, OID_AUTO, reorder_timeout, CTLFLAG_RWTUN,
|
|
|
|
&reorder_timeout, 0, "Reorder timeout is milliseconds");
|
|
|
|
|
|
|
|
/* Packet reorder FIFO queue */
|
|
|
|
struct ng_pptpgre_roq {
|
|
|
|
SLIST_ENTRY(ng_pptpgre_roq) next; /* next entry of the queue */
|
|
|
|
item_p item; /* netgraph item */
|
|
|
|
u_int32_t seq; /* packet sequence number */
|
|
|
|
};
|
|
|
|
SLIST_HEAD(ng_pptpgre_roq_head, ng_pptpgre_roq);
|
|
|
|
typedef struct ng_pptpgre_roq_head roqh;
|
|
|
|
|
1999-12-08 18:55:39 +00:00
|
|
|
/* We keep packet retransmit and acknowlegement state in this struct */
|
2008-03-24 22:55:22 +00:00
|
|
|
struct ng_pptpgre_sess {
|
|
|
|
node_p node; /* this node pointer */
|
|
|
|
hook_p hook; /* hook to upper layers */
|
|
|
|
struct ng_pptpgre_conf conf; /* configuration info */
|
|
|
|
struct mtx mtx; /* session mutex */
|
|
|
|
u_int32_t recvSeq; /* last seq # we rcv'd */
|
|
|
|
u_int32_t xmitSeq; /* last seq # we sent */
|
|
|
|
u_int32_t recvAck; /* last seq # peer ack'd */
|
|
|
|
u_int32_t xmitAck; /* last seq # we ack'd */
|
1999-12-08 18:55:39 +00:00
|
|
|
int32_t ato; /* adaptive time-out value */
|
|
|
|
int32_t rtt; /* round trip time estimate */
|
|
|
|
int32_t dev; /* deviation estimate */
|
|
|
|
u_int16_t xmitWin; /* size of xmit window */
|
2002-04-14 17:37:35 +00:00
|
|
|
struct callout sackTimer; /* send ack timer */
|
|
|
|
struct callout rackTimer; /* recv ack timer */
|
2000-06-26 19:43:24 +00:00
|
|
|
u_int32_t winAck; /* seq when xmitWin will grow */
|
1999-12-08 18:55:39 +00:00
|
|
|
pptptime_t timeSent[PPTP_XMIT_WIN];
|
2008-03-24 22:55:22 +00:00
|
|
|
LIST_ENTRY(ng_pptpgre_sess) sessions;
|
2018-11-04 19:10:44 +00:00
|
|
|
roqh roq; /* reorder queue head */
|
|
|
|
u_int8_t roq_len; /* reorder queue length */
|
|
|
|
struct callout reorderTimer; /* reorder timeout handler */
|
1999-12-08 18:55:39 +00:00
|
|
|
};
|
2008-03-24 22:55:22 +00:00
|
|
|
typedef struct ng_pptpgre_sess *hpriv_p;
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* Node private data */
|
|
|
|
struct ng_pptpgre_private {
|
|
|
|
hook_p upper; /* hook to upper layers */
|
|
|
|
hook_p lower; /* hook to lower layers */
|
2008-03-24 22:55:22 +00:00
|
|
|
struct ng_pptpgre_sess uppersess; /* default session for compat */
|
|
|
|
LIST_HEAD(, ng_pptpgre_sess) sesshash[SESSHASHSIZE];
|
2000-05-05 01:11:39 +00:00
|
|
|
struct ng_pptpgre_stats stats; /* node statistics */
|
1999-12-08 18:55:39 +00:00
|
|
|
};
|
|
|
|
typedef struct ng_pptpgre_private *priv_p;
|
|
|
|
|
|
|
|
/* Netgraph node methods */
|
|
|
|
static ng_constructor_t ng_pptpgre_constructor;
|
|
|
|
static ng_rcvmsg_t ng_pptpgre_rcvmsg;
|
2001-01-06 00:46:47 +00:00
|
|
|
static ng_shutdown_t ng_pptpgre_shutdown;
|
1999-12-08 18:55:39 +00:00
|
|
|
static ng_newhook_t ng_pptpgre_newhook;
|
|
|
|
static ng_rcvdata_t ng_pptpgre_rcvdata;
|
2008-03-24 22:55:22 +00:00
|
|
|
static ng_rcvdata_t ng_pptpgre_rcvdata_lower;
|
1999-12-08 18:55:39 +00:00
|
|
|
static ng_disconnect_t ng_pptpgre_disconnect;
|
|
|
|
|
|
|
|
/* Helper functions */
|
2008-03-24 22:55:22 +00:00
|
|
|
static int ng_pptpgre_xmit(hpriv_p hpriv, item_p item);
|
2008-03-26 21:19:03 +00:00
|
|
|
static void ng_pptpgre_start_send_ack_timer(hpriv_p hpriv);
|
2008-03-24 22:55:22 +00:00
|
|
|
static void ng_pptpgre_start_recv_ack_timer(hpriv_p hpriv);
|
2018-11-04 19:10:44 +00:00
|
|
|
static void ng_pptpgre_start_reorder_timer(hpriv_p hpriv);
|
2004-12-09 07:49:02 +00:00
|
|
|
static void ng_pptpgre_recv_ack_timeout(node_p node, hook_p hook,
|
|
|
|
void *arg1, int arg2);
|
|
|
|
static void ng_pptpgre_send_ack_timeout(node_p node, hook_p hook,
|
|
|
|
void *arg1, int arg2);
|
2018-11-04 19:10:44 +00:00
|
|
|
static void ng_pptpgre_reorder_timeout(node_p node, hook_p hook,
|
|
|
|
void *arg1, int arg2);
|
2008-03-24 22:55:22 +00:00
|
|
|
static hpriv_p ng_pptpgre_find_session(priv_p privp, u_int16_t cid);
|
|
|
|
static void ng_pptpgre_reset(hpriv_p hpriv);
|
|
|
|
static pptptime_t ng_pptpgre_time(void);
|
2018-11-04 19:10:44 +00:00
|
|
|
static void ng_pptpgre_ack(const hpriv_p hpriv);
|
|
|
|
static int ng_pptpgre_sendq(const hpriv_p hpriv, roqh *q,
|
|
|
|
const struct ng_pptpgre_roq *st);
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* Parse type for struct ng_pptpgre_conf */
|
2002-05-31 23:48:03 +00:00
|
|
|
static const struct ng_parse_struct_field ng_pptpgre_conf_type_fields[]
|
|
|
|
= NG_PPTPGRE_CONF_TYPE_INFO;
|
1999-12-08 18:55:39 +00:00
|
|
|
static const struct ng_parse_type ng_pptpgre_conf_type = {
|
|
|
|
&ng_parse_struct_type,
|
2002-05-31 23:48:03 +00:00
|
|
|
&ng_pptpgre_conf_type_fields,
|
1999-12-08 18:55:39 +00:00
|
|
|
};
|
|
|
|
|
2000-05-05 01:11:39 +00:00
|
|
|
/* Parse type for struct ng_pptpgre_stats */
|
2002-05-31 23:48:03 +00:00
|
|
|
static const struct ng_parse_struct_field ng_pptpgre_stats_type_fields[]
|
|
|
|
= NG_PPTPGRE_STATS_TYPE_INFO;
|
2000-05-05 01:11:39 +00:00
|
|
|
static const struct ng_parse_type ng_pptp_stats_type = {
|
|
|
|
&ng_parse_struct_type,
|
2002-05-31 23:48:03 +00:00
|
|
|
&ng_pptpgre_stats_type_fields
|
2000-05-05 01:11:39 +00:00
|
|
|
};
|
|
|
|
|
1999-12-08 18:55:39 +00:00
|
|
|
/* List of commands and how to convert arguments to/from ASCII */
|
|
|
|
static const struct ng_cmdlist ng_pptpgre_cmdlist[] = {
|
|
|
|
{
|
|
|
|
NGM_PPTPGRE_COOKIE,
|
|
|
|
NGM_PPTPGRE_SET_CONFIG,
|
|
|
|
"setconfig",
|
|
|
|
&ng_pptpgre_conf_type,
|
|
|
|
NULL
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_PPTPGRE_COOKIE,
|
|
|
|
NGM_PPTPGRE_GET_CONFIG,
|
|
|
|
"getconfig",
|
2008-03-24 22:55:22 +00:00
|
|
|
&ng_parse_hint16_type,
|
1999-12-08 18:55:39 +00:00
|
|
|
&ng_pptpgre_conf_type
|
|
|
|
},
|
2000-05-05 01:11:39 +00:00
|
|
|
{
|
|
|
|
NGM_PPTPGRE_COOKIE,
|
|
|
|
NGM_PPTPGRE_GET_STATS,
|
|
|
|
"getstats",
|
|
|
|
NULL,
|
|
|
|
&ng_pptp_stats_type
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_PPTPGRE_COOKIE,
|
|
|
|
NGM_PPTPGRE_CLR_STATS,
|
|
|
|
"clrstats",
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_PPTPGRE_COOKIE,
|
|
|
|
NGM_PPTPGRE_GETCLR_STATS,
|
|
|
|
"getclrstats",
|
|
|
|
NULL,
|
|
|
|
&ng_pptp_stats_type
|
|
|
|
},
|
1999-12-08 18:55:39 +00:00
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Node type descriptor */
|
|
|
|
static struct ng_type ng_pptpgre_typestruct = {
|
2004-05-29 00:51:19 +00:00
|
|
|
.version = NG_ABI_VERSION,
|
|
|
|
.name = NG_PPTPGRE_NODE_TYPE,
|
|
|
|
.constructor = ng_pptpgre_constructor,
|
|
|
|
.rcvmsg = ng_pptpgre_rcvmsg,
|
|
|
|
.shutdown = ng_pptpgre_shutdown,
|
|
|
|
.newhook = ng_pptpgre_newhook,
|
|
|
|
.rcvdata = ng_pptpgre_rcvdata,
|
|
|
|
.disconnect = ng_pptpgre_disconnect,
|
|
|
|
.cmdlist = ng_pptpgre_cmdlist,
|
1999-12-08 18:55:39 +00:00
|
|
|
};
|
|
|
|
NETGRAPH_INIT(pptpgre, &ng_pptpgre_typestruct);
|
|
|
|
|
|
|
|
#define ERROUT(x) do { error = (x); goto done; } while (0)
|
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
NETGRAPH NODE STUFF
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Node type constructor
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_pptpgre_constructor(node_p node)
|
1999-12-08 18:55:39 +00:00
|
|
|
{
|
|
|
|
priv_p priv;
|
2008-03-24 22:55:22 +00:00
|
|
|
int i;
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* Allocate private structure */
|
2024-05-31 16:19:54 +00:00
|
|
|
priv = malloc(sizeof(*priv), M_NETGRAPH_PPTP, M_WAITOK | M_ZERO);
|
1999-12-08 18:55:39 +00:00
|
|
|
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_NODE_SET_PRIVATE(node, priv);
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* Initialize state */
|
2008-03-24 22:55:22 +00:00
|
|
|
mtx_init(&priv->uppersess.mtx, "ng_pptp", NULL, MTX_DEF);
|
|
|
|
ng_callout_init(&priv->uppersess.sackTimer);
|
|
|
|
ng_callout_init(&priv->uppersess.rackTimer);
|
|
|
|
priv->uppersess.node = node;
|
|
|
|
|
2018-11-04 19:10:44 +00:00
|
|
|
SLIST_INIT(&priv->uppersess.roq);
|
|
|
|
priv->uppersess.roq_len = 0;
|
|
|
|
ng_callout_init(&priv->uppersess.reorderTimer);
|
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
for (i = 0; i < SESSHASHSIZE; i++)
|
|
|
|
LIST_INIT(&priv->sesshash[i]);
|
|
|
|
|
|
|
|
LIST_INSERT_HEAD(&priv->sesshash[0], &priv->uppersess, sessions);
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Give our OK for a hook to be added.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_pptpgre_newhook(node_p node, hook_p hook, const char *name)
|
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* Check hook name */
|
2008-03-24 22:55:22 +00:00
|
|
|
if (strcmp(name, NG_PPTPGRE_HOOK_UPPER) == 0) {
|
|
|
|
priv->upper = hook;
|
|
|
|
priv->uppersess.hook = hook;
|
|
|
|
NG_HOOK_SET_PRIVATE(hook, &priv->uppersess);
|
|
|
|
} else if (strcmp(name, NG_PPTPGRE_HOOK_LOWER) == 0) {
|
|
|
|
priv->lower = hook;
|
|
|
|
NG_HOOK_SET_RCVDATA(hook, ng_pptpgre_rcvdata_lower);
|
|
|
|
} else {
|
|
|
|
static const char hexdig[16] = "0123456789abcdef";
|
|
|
|
const char *hex;
|
|
|
|
hpriv_p hpriv;
|
|
|
|
int i, j;
|
|
|
|
uint16_t cid, hash;
|
|
|
|
|
|
|
|
/* Parse hook name to get session ID */
|
|
|
|
if (strncmp(name, NG_PPTPGRE_HOOK_SESSION_P,
|
|
|
|
sizeof(NG_PPTPGRE_HOOK_SESSION_P) - 1) != 0)
|
|
|
|
return (EINVAL);
|
|
|
|
hex = name + sizeof(NG_PPTPGRE_HOOK_SESSION_P) - 1;
|
|
|
|
for (cid = i = 0; i < 4; i++) {
|
|
|
|
for (j = 0; j < 16 && hex[i] != hexdig[j]; j++);
|
|
|
|
if (j == 16)
|
|
|
|
return (EINVAL);
|
|
|
|
cid = (cid << 4) | j;
|
|
|
|
}
|
|
|
|
if (hex[i] != '\0')
|
|
|
|
return (EINVAL);
|
|
|
|
|
2024-05-31 16:19:54 +00:00
|
|
|
hpriv = malloc(sizeof(*hpriv), M_NETGRAPH_PPTP, M_NOWAIT | M_ZERO);
|
2008-03-24 22:55:22 +00:00
|
|
|
if (hpriv == NULL)
|
|
|
|
return (ENOMEM);
|
2020-09-01 21:19:14 +00:00
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
/* Initialize state */
|
|
|
|
mtx_init(&hpriv->mtx, "ng_pptp", NULL, MTX_DEF);
|
|
|
|
ng_callout_init(&hpriv->sackTimer);
|
|
|
|
ng_callout_init(&hpriv->rackTimer);
|
|
|
|
hpriv->conf.cid = cid;
|
|
|
|
hpriv->node = node;
|
|
|
|
hpriv->hook = hook;
|
2018-11-04 19:10:44 +00:00
|
|
|
|
|
|
|
SLIST_INIT(&hpriv->roq);
|
|
|
|
hpriv->roq_len = 0;
|
|
|
|
ng_callout_init(&hpriv->reorderTimer);
|
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
NG_HOOK_SET_PRIVATE(hook, hpriv);
|
|
|
|
|
|
|
|
hash = SESSHASH(cid);
|
|
|
|
LIST_INSERT_HEAD(&priv->sesshash[hash], hpriv, sessions);
|
|
|
|
}
|
|
|
|
|
1999-12-08 18:55:39 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive a control message.
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_pptpgre_rcvmsg(node_p node, item_p item, hook_p lasthook)
|
1999-12-08 18:55:39 +00:00
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
1999-12-08 18:55:39 +00:00
|
|
|
struct ng_mesg *resp = NULL;
|
|
|
|
int error = 0;
|
2001-01-06 00:46:47 +00:00
|
|
|
struct ng_mesg *msg;
|
1999-12-08 18:55:39 +00:00
|
|
|
|
2001-01-06 00:46:47 +00:00
|
|
|
NGI_GET_MSG(item, msg);
|
1999-12-08 18:55:39 +00:00
|
|
|
switch (msg->header.typecookie) {
|
|
|
|
case NGM_PPTPGRE_COOKIE:
|
|
|
|
switch (msg->header.cmd) {
|
|
|
|
case NGM_PPTPGRE_SET_CONFIG:
|
|
|
|
{
|
|
|
|
struct ng_pptpgre_conf *const newConf =
|
|
|
|
(struct ng_pptpgre_conf *) msg->data;
|
2008-03-24 22:55:22 +00:00
|
|
|
hpriv_p hpriv;
|
|
|
|
uint16_t hash;
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* Check for invalid or illegal config */
|
|
|
|
if (msg->header.arglen != sizeof(*newConf))
|
|
|
|
ERROUT(EINVAL);
|
2008-03-24 22:55:22 +00:00
|
|
|
/* Try to find session by cid. */
|
|
|
|
hpriv = ng_pptpgre_find_session(priv, newConf->cid);
|
|
|
|
/* If not present - use upper. */
|
|
|
|
if (hpriv == NULL) {
|
|
|
|
hpriv = &priv->uppersess;
|
|
|
|
LIST_REMOVE(hpriv, sessions);
|
|
|
|
hash = SESSHASH(newConf->cid);
|
|
|
|
LIST_INSERT_HEAD(&priv->sesshash[hash], hpriv,
|
|
|
|
sessions);
|
|
|
|
}
|
|
|
|
ng_pptpgre_reset(hpriv); /* reset on configure */
|
|
|
|
hpriv->conf = *newConf;
|
1999-12-08 18:55:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NGM_PPTPGRE_GET_CONFIG:
|
2008-03-24 22:55:22 +00:00
|
|
|
{
|
|
|
|
hpriv_p hpriv;
|
|
|
|
|
|
|
|
if (msg->header.arglen == 2) {
|
|
|
|
/* Try to find session by cid. */
|
|
|
|
hpriv = ng_pptpgre_find_session(priv,
|
|
|
|
*((uint16_t *)msg->data));
|
|
|
|
if (hpriv == NULL)
|
|
|
|
ERROUT(EINVAL);
|
|
|
|
} else if (msg->header.arglen == 0) {
|
|
|
|
/* Use upper. */
|
|
|
|
hpriv = &priv->uppersess;
|
|
|
|
} else
|
|
|
|
ERROUT(EINVAL);
|
|
|
|
NG_MKRESPONSE(resp, msg, sizeof(hpriv->conf), M_NOWAIT);
|
1999-12-08 18:55:39 +00:00
|
|
|
if (resp == NULL)
|
|
|
|
ERROUT(ENOMEM);
|
2008-03-24 22:55:22 +00:00
|
|
|
bcopy(&hpriv->conf, resp->data, sizeof(hpriv->conf));
|
1999-12-08 18:55:39 +00:00
|
|
|
break;
|
2008-03-24 22:55:22 +00:00
|
|
|
}
|
2000-05-05 01:11:39 +00:00
|
|
|
case NGM_PPTPGRE_GET_STATS:
|
|
|
|
case NGM_PPTPGRE_CLR_STATS:
|
|
|
|
case NGM_PPTPGRE_GETCLR_STATS:
|
|
|
|
{
|
|
|
|
if (msg->header.cmd != NGM_PPTPGRE_CLR_STATS) {
|
|
|
|
NG_MKRESPONSE(resp, msg,
|
|
|
|
sizeof(priv->stats), M_NOWAIT);
|
|
|
|
if (resp == NULL)
|
|
|
|
ERROUT(ENOMEM);
|
|
|
|
bcopy(&priv->stats,
|
|
|
|
resp->data, sizeof(priv->stats));
|
|
|
|
}
|
|
|
|
if (msg->header.cmd != NGM_PPTPGRE_GET_STATS)
|
|
|
|
bzero(&priv->stats, sizeof(priv->stats));
|
|
|
|
break;
|
|
|
|
}
|
1999-12-08 18:55:39 +00:00
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2000-12-18 20:03:32 +00:00
|
|
|
done:
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_RESPOND_MSG(error, node, item, resp);
|
|
|
|
NG_FREE_MSG(msg);
|
1999-12-08 18:55:39 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive incoming data on a hook.
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_pptpgre_rcvdata(hook_p hook, item_p item)
|
1999-12-08 18:55:39 +00:00
|
|
|
{
|
2008-03-24 22:55:22 +00:00
|
|
|
const hpriv_p hpriv = NG_HOOK_PRIVATE(hook);
|
2005-08-30 09:51:54 +00:00
|
|
|
int rval;
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* If not configured, reject */
|
2008-03-24 22:55:22 +00:00
|
|
|
if (!hpriv->conf.enabled) {
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_ITEM(item);
|
1999-12-08 18:55:39 +00:00
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
mtx_lock(&hpriv->mtx);
|
2005-08-30 09:51:54 +00:00
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
rval = ng_pptpgre_xmit(hpriv, item);
|
2005-08-30 09:51:54 +00:00
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
mtx_assert(&hpriv->mtx, MA_NOTOWNED);
|
2005-08-30 09:51:54 +00:00
|
|
|
|
|
|
|
return (rval);
|
1999-12-08 18:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-03-24 22:55:22 +00:00
|
|
|
* Hook disconnection
|
1999-12-08 18:55:39 +00:00
|
|
|
*/
|
|
|
|
static int
|
2008-03-24 22:55:22 +00:00
|
|
|
ng_pptpgre_disconnect(hook_p hook)
|
1999-12-08 18:55:39 +00:00
|
|
|
{
|
2008-03-24 22:55:22 +00:00
|
|
|
const node_p node = NG_HOOK_NODE(hook);
|
2001-01-08 05:34:06 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
2008-03-24 22:55:22 +00:00
|
|
|
const hpriv_p hpriv = NG_HOOK_PRIVATE(hook);
|
1999-12-08 18:55:39 +00:00
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
/* Zero out hook pointer */
|
|
|
|
if (hook == priv->upper) {
|
|
|
|
priv->upper = NULL;
|
|
|
|
priv->uppersess.hook = NULL;
|
|
|
|
} else if (hook == priv->lower) {
|
|
|
|
priv->lower = NULL;
|
|
|
|
} else {
|
|
|
|
/* Reset node (stops timers) */
|
|
|
|
ng_pptpgre_reset(hpriv);
|
2005-08-30 09:51:54 +00:00
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
LIST_REMOVE(hpriv, sessions);
|
|
|
|
mtx_destroy(&hpriv->mtx);
|
2024-05-31 16:19:54 +00:00
|
|
|
free(hpriv, M_NETGRAPH_PPTP);
|
2008-03-24 22:55:22 +00:00
|
|
|
}
|
2002-04-14 17:37:35 +00:00
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
/* Go away if no longer connected to anything */
|
|
|
|
if ((NG_NODE_NUMHOOKS(node) == 0)
|
|
|
|
&& (NG_NODE_IS_VALID(node)))
|
|
|
|
ng_rmnode_self(node);
|
1999-12-08 18:55:39 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-03-24 22:55:22 +00:00
|
|
|
* Destroy node
|
1999-12-08 18:55:39 +00:00
|
|
|
*/
|
|
|
|
static int
|
2008-03-24 22:55:22 +00:00
|
|
|
ng_pptpgre_shutdown(node_p node)
|
1999-12-08 18:55:39 +00:00
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
1999-12-08 18:55:39 +00:00
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
/* Reset node (stops timers) */
|
|
|
|
ng_pptpgre_reset(&priv->uppersess);
|
1999-12-08 18:55:39 +00:00
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
LIST_REMOVE(&priv->uppersess, sessions);
|
|
|
|
mtx_destroy(&priv->uppersess.mtx);
|
|
|
|
|
2024-05-31 16:19:54 +00:00
|
|
|
free(priv, M_NETGRAPH_PPTP);
|
2008-03-24 22:55:22 +00:00
|
|
|
|
|
|
|
/* Decrement ref count */
|
|
|
|
NG_NODE_UNREF(node);
|
1999-12-08 18:55:39 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
TRANSMIT AND RECEIVE FUNCTIONS
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Transmit an outgoing frame, or just an ack if m is NULL.
|
|
|
|
*/
|
|
|
|
static int
|
2008-03-24 22:55:22 +00:00
|
|
|
ng_pptpgre_xmit(hpriv_p hpriv, item_p item)
|
1999-12-08 18:55:39 +00:00
|
|
|
{
|
2008-03-24 22:55:22 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(hpriv->node);
|
1999-12-08 18:55:39 +00:00
|
|
|
u_char buf[sizeof(struct greheader) + 2 * sizeof(u_int32_t)];
|
|
|
|
struct greheader *const gre = (struct greheader *)buf;
|
|
|
|
int grelen, error;
|
2001-01-06 00:46:47 +00:00
|
|
|
struct mbuf *m;
|
1999-12-08 18:55:39 +00:00
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
mtx_assert(&hpriv->mtx, MA_OWNED);
|
2005-09-08 14:26:23 +00:00
|
|
|
|
2001-01-06 00:46:47 +00:00
|
|
|
if (item) {
|
|
|
|
NGI_GET_M(item, m);
|
|
|
|
} else {
|
|
|
|
m = NULL;
|
|
|
|
}
|
2000-05-05 01:11:39 +00:00
|
|
|
/* Check if there's data */
|
|
|
|
if (m != NULL) {
|
2004-04-26 14:26:54 +00:00
|
|
|
/* Check if windowing is enabled */
|
2008-03-24 22:55:22 +00:00
|
|
|
if (hpriv->conf.enableWindowing) {
|
2004-04-26 14:26:54 +00:00
|
|
|
/* Is our transmit window full? */
|
2008-03-24 22:55:22 +00:00
|
|
|
if ((u_int32_t)PPTP_SEQ_DIFF(hpriv->xmitSeq,
|
|
|
|
hpriv->recvAck) >= hpriv->xmitWin) {
|
2004-04-26 14:26:54 +00:00
|
|
|
priv->stats.xmitDrops++;
|
2005-09-08 14:26:23 +00:00
|
|
|
ERROUT(ENOBUFS);
|
2004-04-26 14:26:54 +00:00
|
|
|
}
|
2000-05-05 01:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sanity check frame length */
|
2012-08-03 12:55:31 +00:00
|
|
|
if (m->m_pkthdr.len > PPTP_MAX_PAYLOAD) {
|
2000-05-05 01:11:39 +00:00
|
|
|
priv->stats.xmitTooBig++;
|
2005-09-08 14:26:23 +00:00
|
|
|
ERROUT(EMSGSIZE);
|
2000-05-05 01:11:39 +00:00
|
|
|
}
|
2001-01-06 00:46:47 +00:00
|
|
|
} else {
|
2000-05-05 01:11:39 +00:00
|
|
|
priv->stats.xmitLoneAcks++;
|
2001-01-06 00:46:47 +00:00
|
|
|
}
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* Build GRE header */
|
2010-04-01 16:20:36 +00:00
|
|
|
be32enc(gre, PPTP_INIT_VALUE);
|
|
|
|
be16enc(&gre->length, (m != NULL) ? m->m_pkthdr.len : 0);
|
|
|
|
be16enc(&gre->cid, hpriv->conf.peerCid);
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* Include sequence number if packet contains any data */
|
|
|
|
if (m != NULL) {
|
|
|
|
gre->hasSeq = 1;
|
2008-03-24 22:55:22 +00:00
|
|
|
if (hpriv->conf.enableWindowing) {
|
|
|
|
hpriv->timeSent[hpriv->xmitSeq - hpriv->recvAck]
|
|
|
|
= ng_pptpgre_time();
|
2004-04-26 14:26:54 +00:00
|
|
|
}
|
2008-03-24 22:55:22 +00:00
|
|
|
hpriv->xmitSeq++;
|
2010-04-01 16:20:36 +00:00
|
|
|
be32enc(&gre->data[0], hpriv->xmitSeq);
|
1999-12-08 18:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Include acknowledgement (and stop send ack timer) if needed */
|
2008-03-24 22:55:22 +00:00
|
|
|
if (hpriv->conf.enableAlwaysAck || hpriv->xmitAck != hpriv->recvSeq) {
|
1999-12-08 18:55:39 +00:00
|
|
|
gre->hasAck = 1;
|
2010-04-01 16:20:36 +00:00
|
|
|
be32enc(&gre->data[gre->hasSeq], hpriv->recvSeq);
|
2008-03-24 22:55:22 +00:00
|
|
|
hpriv->xmitAck = hpriv->recvSeq;
|
2008-03-26 21:19:03 +00:00
|
|
|
if (hpriv->conf.enableDelayedAck)
|
|
|
|
ng_uncallout(&hpriv->sackTimer, hpriv->node);
|
1999-12-08 18:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepend GRE header to outgoing frame */
|
|
|
|
grelen = sizeof(*gre) + sizeof(u_int32_t) * (gre->hasSeq + gre->hasAck);
|
|
|
|
if (m == NULL) {
|
2012-12-05 08:04:20 +00:00
|
|
|
MGETHDR(m, M_NOWAIT, MT_DATA);
|
1999-12-08 18:55:39 +00:00
|
|
|
if (m == NULL) {
|
2000-07-25 00:23:19 +00:00
|
|
|
priv->stats.memoryFailures++;
|
2005-09-08 14:26:23 +00:00
|
|
|
ERROUT(ENOBUFS);
|
1999-12-08 18:55:39 +00:00
|
|
|
}
|
|
|
|
m->m_len = m->m_pkthdr.len = grelen;
|
|
|
|
m->m_pkthdr.rcvif = NULL;
|
|
|
|
} else {
|
2012-12-05 08:04:20 +00:00
|
|
|
M_PREPEND(m, grelen, M_NOWAIT);
|
1999-12-08 18:55:39 +00:00
|
|
|
if (m == NULL || (m->m_len < grelen
|
|
|
|
&& (m = m_pullup(m, grelen)) == NULL)) {
|
2000-07-25 00:23:19 +00:00
|
|
|
priv->stats.memoryFailures++;
|
2005-09-08 14:26:23 +00:00
|
|
|
ERROUT(ENOBUFS);
|
1999-12-08 18:55:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bcopy(gre, mtod(m, u_char *), grelen);
|
|
|
|
|
2000-05-05 01:11:39 +00:00
|
|
|
/* Update stats */
|
|
|
|
priv->stats.xmitPackets++;
|
|
|
|
priv->stats.xmitOctets += m->m_pkthdr.len;
|
|
|
|
|
2005-09-08 14:26:23 +00:00
|
|
|
/*
|
|
|
|
* XXX: we should reset timer only after an item has been sent
|
|
|
|
* successfully.
|
|
|
|
*/
|
2008-03-24 22:55:22 +00:00
|
|
|
if (hpriv->conf.enableWindowing &&
|
|
|
|
gre->hasSeq && hpriv->xmitSeq == hpriv->recvAck + 1)
|
|
|
|
ng_pptpgre_start_recv_ack_timer(hpriv);
|
2005-09-08 14:26:23 +00:00
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
mtx_unlock(&hpriv->mtx);
|
2005-09-08 14:26:23 +00:00
|
|
|
|
1999-12-08 18:55:39 +00:00
|
|
|
/* Deliver packet */
|
2001-01-06 00:46:47 +00:00
|
|
|
if (item) {
|
|
|
|
NG_FWD_NEW_DATA(error, item, priv->lower, m);
|
|
|
|
} else {
|
|
|
|
NG_SEND_DATA_ONLY(error, priv->lower, m);
|
|
|
|
}
|
|
|
|
|
2005-09-08 14:26:23 +00:00
|
|
|
return (error);
|
2000-07-25 00:23:19 +00:00
|
|
|
|
2005-09-08 14:26:23 +00:00
|
|
|
done:
|
2008-03-24 22:55:22 +00:00
|
|
|
mtx_unlock(&hpriv->mtx);
|
2005-09-08 14:26:23 +00:00
|
|
|
NG_FREE_M(m);
|
|
|
|
if (item)
|
|
|
|
NG_FREE_ITEM(item);
|
1999-12-08 18:55:39 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2018-11-04 19:10:44 +00:00
|
|
|
static void
|
|
|
|
ng_pptpgre_ack(const hpriv_p hpriv)
|
|
|
|
{
|
|
|
|
mtx_assert(&hpriv->mtx, MA_OWNED);
|
|
|
|
if (!(callout_pending(&hpriv->sackTimer))) {
|
|
|
|
/* If delayed ACK is disabled, send it now */
|
|
|
|
if (!hpriv->conf.enableDelayedAck) { /* ack now */
|
|
|
|
ng_pptpgre_xmit(hpriv, NULL);
|
|
|
|
/* ng_pptpgre_xmit() drops the mutex */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* ack later */
|
|
|
|
ng_pptpgre_start_send_ack_timer(hpriv);
|
|
|
|
mtx_unlock(&hpriv->mtx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mtx_unlock(&hpriv->mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delivers packets from the queue "q" to upper layers. Frees delivered
|
|
|
|
* entries with the exception of one equal to "st" that is allocated
|
|
|
|
* on caller's stack and not on the heap.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_pptpgre_sendq(const hpriv_p hpriv, roqh *q, const struct ng_pptpgre_roq *st)
|
|
|
|
{
|
|
|
|
struct ng_pptpgre_roq *np;
|
|
|
|
struct mbuf *m;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
mtx_assert(&hpriv->mtx, MA_NOTOWNED);
|
|
|
|
while (!SLIST_EMPTY(q)) {
|
|
|
|
np = SLIST_FIRST(q);
|
|
|
|
SLIST_REMOVE_HEAD(q, next);
|
|
|
|
NGI_GET_M(np->item, m);
|
|
|
|
NG_FWD_NEW_DATA(error, np->item, hpriv->hook, m);
|
|
|
|
if (np != st)
|
2024-05-31 16:19:54 +00:00
|
|
|
free(np, M_NETGRAPH_PPTP);
|
2018-11-04 19:10:44 +00:00
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1999-12-08 18:55:39 +00:00
|
|
|
/*
|
|
|
|
* Handle an incoming packet. The packet includes the IP header.
|
|
|
|
*/
|
|
|
|
static int
|
2008-03-24 22:55:22 +00:00
|
|
|
ng_pptpgre_rcvdata_lower(hook_p hook, item_p item)
|
1999-12-08 18:55:39 +00:00
|
|
|
{
|
2008-03-24 22:55:22 +00:00
|
|
|
hpriv_p hpriv;
|
|
|
|
node_p node = NG_HOOK_NODE(hook);
|
2001-01-08 05:34:06 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
1999-12-08 18:55:39 +00:00
|
|
|
int iphlen, grelen, extralen;
|
2002-06-05 23:35:31 +00:00
|
|
|
const struct greheader *gre;
|
|
|
|
const struct ip *ip;
|
1999-12-08 18:55:39 +00:00
|
|
|
int error = 0;
|
2001-01-06 00:46:47 +00:00
|
|
|
struct mbuf *m;
|
1999-12-08 18:55:39 +00:00
|
|
|
|
2018-11-04 19:10:44 +00:00
|
|
|
roqh sendq = SLIST_HEAD_INITIALIZER(sendq); /* send queue on stack */
|
|
|
|
struct ng_pptpgre_roq *last = NULL; /* last packet in the sendq */
|
|
|
|
struct ng_pptpgre_roq *np, *prev;
|
|
|
|
struct ng_pptpgre_roq temp = { { NULL }, NULL, 0 };
|
|
|
|
long diff;
|
|
|
|
u_int32_t seq;
|
|
|
|
|
|
|
|
m = NGI_M(item);
|
2000-05-05 01:11:39 +00:00
|
|
|
/* Update stats */
|
|
|
|
priv->stats.recvPackets++;
|
|
|
|
priv->stats.recvOctets += m->m_pkthdr.len;
|
|
|
|
|
1999-12-08 18:55:39 +00:00
|
|
|
/* Sanity check packet length */
|
|
|
|
if (m->m_pkthdr.len < sizeof(*ip) + sizeof(*gre)) {
|
2000-05-05 01:11:39 +00:00
|
|
|
priv->stats.recvRunts++;
|
2005-09-08 14:26:23 +00:00
|
|
|
ERROUT(EINVAL);
|
1999-12-08 18:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Safely pull up the complete IP+GRE headers */
|
2018-11-04 19:10:44 +00:00
|
|
|
if (m->m_len < sizeof(*ip) + sizeof(*gre)) {
|
|
|
|
if ((m = m_pullup(m, sizeof(*ip) + sizeof(*gre))) == NULL) {
|
|
|
|
priv->stats.memoryFailures++;
|
|
|
|
_NGI_M(item) = NULL;
|
|
|
|
ERROUT(ENOBUFS);
|
|
|
|
}
|
|
|
|
_NGI_M(item) = m;
|
1999-12-08 18:55:39 +00:00
|
|
|
}
|
2002-06-05 23:35:31 +00:00
|
|
|
ip = mtod(m, const struct ip *);
|
1999-12-08 18:55:39 +00:00
|
|
|
iphlen = ip->ip_hl << 2;
|
|
|
|
if (m->m_len < iphlen + sizeof(*gre)) {
|
|
|
|
if ((m = m_pullup(m, iphlen + sizeof(*gre))) == NULL) {
|
2000-07-25 00:23:19 +00:00
|
|
|
priv->stats.memoryFailures++;
|
2018-11-04 19:10:44 +00:00
|
|
|
_NGI_M(item) = NULL;
|
2005-09-08 14:26:23 +00:00
|
|
|
ERROUT(ENOBUFS);
|
1999-12-08 18:55:39 +00:00
|
|
|
}
|
2018-11-04 19:10:44 +00:00
|
|
|
_NGI_M(item) = m;
|
2002-06-05 23:35:31 +00:00
|
|
|
ip = mtod(m, const struct ip *);
|
1999-12-08 18:55:39 +00:00
|
|
|
}
|
2002-06-05 23:35:31 +00:00
|
|
|
gre = (const struct greheader *)((const u_char *)ip + iphlen);
|
1999-12-08 18:55:39 +00:00
|
|
|
grelen = sizeof(*gre) + sizeof(u_int32_t) * (gre->hasSeq + gre->hasAck);
|
2000-05-05 01:11:39 +00:00
|
|
|
if (m->m_pkthdr.len < iphlen + grelen) {
|
|
|
|
priv->stats.recvRunts++;
|
2005-09-08 14:26:23 +00:00
|
|
|
ERROUT(EINVAL);
|
2000-05-05 01:11:39 +00:00
|
|
|
}
|
1999-12-08 18:55:39 +00:00
|
|
|
if (m->m_len < iphlen + grelen) {
|
|
|
|
if ((m = m_pullup(m, iphlen + grelen)) == NULL) {
|
2000-07-25 00:23:19 +00:00
|
|
|
priv->stats.memoryFailures++;
|
2018-11-04 19:10:44 +00:00
|
|
|
_NGI_M(item) = NULL;
|
2005-09-08 14:26:23 +00:00
|
|
|
ERROUT(ENOBUFS);
|
1999-12-08 18:55:39 +00:00
|
|
|
}
|
2018-11-04 19:10:44 +00:00
|
|
|
_NGI_M(item) = m;
|
2002-06-05 23:35:31 +00:00
|
|
|
ip = mtod(m, const struct ip *);
|
|
|
|
gre = (const struct greheader *)((const u_char *)ip + iphlen);
|
1999-12-08 18:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sanity check packet length and GRE header bits */
|
|
|
|
extralen = m->m_pkthdr.len
|
2010-04-01 16:20:36 +00:00
|
|
|
- (iphlen + grelen + gre->hasSeq * be16dec(&gre->length));
|
2000-05-05 01:11:39 +00:00
|
|
|
if (extralen < 0) {
|
|
|
|
priv->stats.recvBadGRE++;
|
2005-09-08 14:26:23 +00:00
|
|
|
ERROUT(EINVAL);
|
2000-05-05 01:11:39 +00:00
|
|
|
}
|
2010-04-01 16:20:36 +00:00
|
|
|
if ((be32dec(gre) & PPTP_INIT_MASK) != PPTP_INIT_VALUE) {
|
2000-05-05 01:11:39 +00:00
|
|
|
priv->stats.recvBadGRE++;
|
2005-09-08 14:26:23 +00:00
|
|
|
ERROUT(EINVAL);
|
2000-05-05 01:11:39 +00:00
|
|
|
}
|
2008-03-24 22:55:22 +00:00
|
|
|
|
2010-04-01 16:20:36 +00:00
|
|
|
hpriv = ng_pptpgre_find_session(priv, be16dec(&gre->cid));
|
2008-03-24 22:55:22 +00:00
|
|
|
if (hpriv == NULL || hpriv->hook == NULL || !hpriv->conf.enabled) {
|
2000-05-05 01:11:39 +00:00
|
|
|
priv->stats.recvBadCID++;
|
2005-09-08 14:26:23 +00:00
|
|
|
ERROUT(EINVAL);
|
2000-05-05 01:11:39 +00:00
|
|
|
}
|
2008-03-24 22:55:22 +00:00
|
|
|
mtx_lock(&hpriv->mtx);
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* Look for peer ack */
|
|
|
|
if (gre->hasAck) {
|
2010-04-01 16:20:36 +00:00
|
|
|
const u_int32_t ack = be32dec(&gre->data[gre->hasSeq]);
|
2008-03-24 22:55:22 +00:00
|
|
|
const int index = ack - hpriv->recvAck - 1;
|
2001-03-08 20:10:02 +00:00
|
|
|
long sample;
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* Sanity check ack value */
|
2008-03-24 22:55:22 +00:00
|
|
|
if (PPTP_SEQ_DIFF(ack, hpriv->xmitSeq) > 0) {
|
2000-05-05 01:11:39 +00:00
|
|
|
priv->stats.recvBadAcks++;
|
|
|
|
goto badAck; /* we never sent it! */
|
|
|
|
}
|
2008-03-24 22:55:22 +00:00
|
|
|
if (PPTP_SEQ_DIFF(ack, hpriv->recvAck) <= 0)
|
2000-05-05 01:11:39 +00:00
|
|
|
goto badAck; /* ack already timed out */
|
2008-03-24 22:55:22 +00:00
|
|
|
hpriv->recvAck = ack;
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* Update adaptive timeout stuff */
|
2008-03-24 22:55:22 +00:00
|
|
|
if (hpriv->conf.enableWindowing) {
|
|
|
|
sample = ng_pptpgre_time() - hpriv->timeSent[index];
|
|
|
|
diff = sample - hpriv->rtt;
|
|
|
|
hpriv->rtt += PPTP_ACK_ALPHA(diff);
|
2004-04-26 14:26:54 +00:00
|
|
|
if (diff < 0)
|
|
|
|
diff = -diff;
|
2008-03-24 22:55:22 +00:00
|
|
|
hpriv->dev += PPTP_ACK_BETA(diff - hpriv->dev);
|
2007-02-02 09:45:23 +00:00
|
|
|
/* +2 to compensate low precision of int math */
|
2008-03-24 22:55:22 +00:00
|
|
|
hpriv->ato = hpriv->rtt + PPTP_ACK_CHI(hpriv->dev + 2);
|
|
|
|
if (hpriv->ato > PPTP_MAX_TIMEOUT)
|
|
|
|
hpriv->ato = PPTP_MAX_TIMEOUT;
|
|
|
|
else if (hpriv->ato < PPTP_MIN_TIMEOUT)
|
|
|
|
hpriv->ato = PPTP_MIN_TIMEOUT;
|
2004-04-26 14:26:54 +00:00
|
|
|
|
|
|
|
/* Shift packet transmit times in our transmit window */
|
2008-03-24 22:55:22 +00:00
|
|
|
bcopy(hpriv->timeSent + index + 1, hpriv->timeSent,
|
|
|
|
sizeof(*hpriv->timeSent)
|
2004-04-26 14:26:54 +00:00
|
|
|
* (PPTP_XMIT_WIN - (index + 1)));
|
|
|
|
|
|
|
|
/* If we sent an entire window, increase window size */
|
2008-03-24 22:55:22 +00:00
|
|
|
if (PPTP_SEQ_DIFF(ack, hpriv->winAck) >= 0
|
|
|
|
&& hpriv->xmitWin < PPTP_XMIT_WIN) {
|
|
|
|
hpriv->xmitWin++;
|
|
|
|
hpriv->winAck = ack + hpriv->xmitWin;
|
2004-04-26 14:26:54 +00:00
|
|
|
}
|
1999-12-08 18:55:39 +00:00
|
|
|
|
2004-04-26 14:26:54 +00:00
|
|
|
/* Stop/(re)start receive ACK timer as necessary */
|
2008-03-26 21:19:03 +00:00
|
|
|
ng_uncallout(&hpriv->rackTimer, hpriv->node);
|
2008-03-24 22:55:22 +00:00
|
|
|
if (hpriv->recvAck != hpriv->xmitSeq)
|
|
|
|
ng_pptpgre_start_recv_ack_timer(hpriv);
|
2004-04-26 14:26:54 +00:00
|
|
|
}
|
1999-12-08 18:55:39 +00:00
|
|
|
}
|
2000-05-05 01:11:39 +00:00
|
|
|
badAck:
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* See if frame contains any data */
|
2018-11-04 19:10:44 +00:00
|
|
|
if (!gre->hasSeq) { /* no data to deliver */
|
|
|
|
priv->stats.recvLoneAcks++;
|
|
|
|
mtx_unlock(&hpriv->mtx);
|
|
|
|
ERROUT(0);
|
|
|
|
}
|
1999-12-08 18:55:39 +00:00
|
|
|
|
2018-11-04 19:10:44 +00:00
|
|
|
seq = be32dec(&gre->data[0]);
|
|
|
|
|
|
|
|
diff = PPTP_SEQ_DIFF(seq, hpriv->recvSeq);
|
|
|
|
if (diff <= 0) { /* late or duplicate packet */
|
|
|
|
if (diff < 0 && reorder_max == 0) /* reorder disabled */
|
|
|
|
priv->stats.recvOutOfOrder++; /* late */
|
|
|
|
else
|
|
|
|
priv->stats.recvDuplicates++; /* duplicate */
|
|
|
|
mtx_unlock(&hpriv->mtx);
|
|
|
|
ERROUT(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Trim mbuf down to internal payload */
|
|
|
|
m_adj(m, iphlen + grelen);
|
|
|
|
if (extralen > 0)
|
|
|
|
m_adj(m, -extralen);
|
|
|
|
|
|
|
|
#define INIT_SENDQ(t) do { \
|
|
|
|
t.item = item; \
|
|
|
|
t.seq = seq; \
|
|
|
|
SLIST_INSERT_HEAD(&sendq, &t, next); \
|
|
|
|
last = &t; \
|
|
|
|
hpriv->recvSeq = seq; \
|
|
|
|
goto deliver; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
if (diff == 1)
|
|
|
|
/* the packet came in order, place it at the start of sendq */
|
|
|
|
INIT_SENDQ(temp);
|
|
|
|
|
|
|
|
/* The packet came too early, try to enqueue it.
|
|
|
|
*
|
|
|
|
* Check for duplicate in the queue. After this loop, "prev" will be
|
|
|
|
* NULL if the packet should become new head of the queue,
|
|
|
|
* or else it should be inserted after the "prev".
|
|
|
|
*/
|
|
|
|
prev = SLIST_FIRST(&hpriv->roq);
|
|
|
|
SLIST_FOREACH(np, &hpriv->roq, next) {
|
|
|
|
diff = PPTP_SEQ_DIFF(np->seq, seq);
|
|
|
|
if (diff == 0) { /* do not add duplicate, drop it */
|
|
|
|
priv->stats.recvDuplicates++;
|
2008-03-24 22:55:22 +00:00
|
|
|
mtx_unlock(&hpriv->mtx);
|
2005-09-08 14:26:23 +00:00
|
|
|
ERROUT(EINVAL);
|
2000-05-05 01:11:39 +00:00
|
|
|
}
|
2018-11-04 19:10:44 +00:00
|
|
|
if (diff > 0) { /* we found newer packet */
|
|
|
|
if (np == prev) /* that is the head of the queue */
|
|
|
|
prev = NULL; /* put current packet to the head */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev = np;
|
|
|
|
}
|
1999-12-08 18:55:39 +00:00
|
|
|
|
2018-11-04 19:10:44 +00:00
|
|
|
priv->stats.recvOutOfOrder++; /* duplicate not found */
|
|
|
|
if (hpriv->roq_len < reorder_max)
|
|
|
|
goto enqueue; /* reorder enabled and there is a room */
|
1999-12-08 18:55:39 +00:00
|
|
|
|
2018-11-04 19:10:44 +00:00
|
|
|
/*
|
|
|
|
* There is no room in the queue or reorder disabled.
|
|
|
|
*
|
|
|
|
* It the latter case, we may still have non-empty reorder queue
|
|
|
|
* if reorder was disabled in process of reordering.
|
|
|
|
* Then we correctly deliver the queue without growing it.
|
|
|
|
*
|
|
|
|
* In both cases, no malloc()'s until the queue is shortened.
|
|
|
|
*/
|
|
|
|
priv->stats.recvReorderOverflow++;
|
|
|
|
if (prev == NULL) { /* new packet goes before the head */
|
|
|
|
INIT_SENDQ(temp); /* of reorder queue, so put it to sendq */
|
|
|
|
}
|
|
|
|
#undef INIT_SENDQ
|
1999-12-08 18:55:39 +00:00
|
|
|
|
2018-11-04 19:10:44 +00:00
|
|
|
/*
|
|
|
|
* Current packet goes after the head of reorder queue.
|
|
|
|
* Move the head to sendq to make room for current packet.
|
|
|
|
*/
|
|
|
|
np = SLIST_FIRST(&hpriv->roq);
|
|
|
|
if (prev == np)
|
|
|
|
prev = NULL;
|
|
|
|
SLIST_REMOVE_HEAD(&hpriv->roq, next);
|
|
|
|
hpriv->roq_len--; /* we are allowed to use malloc() now */
|
|
|
|
SLIST_INSERT_HEAD(&sendq, np, next);
|
|
|
|
last = np;
|
|
|
|
hpriv->recvSeq = np->seq;
|
|
|
|
|
|
|
|
enqueue:
|
2024-05-31 16:19:54 +00:00
|
|
|
np = malloc(sizeof(*np), M_NETGRAPH_PPTP, M_NOWAIT | M_ZERO);
|
2018-11-04 19:10:44 +00:00
|
|
|
if (np == NULL) {
|
|
|
|
priv->stats.memoryFailures++;
|
|
|
|
/*
|
|
|
|
* Emergency: we cannot save new data.
|
|
|
|
* Flush the queue delivering all queued packets preceeding
|
|
|
|
* current one despite of gaps.
|
|
|
|
*/
|
|
|
|
while (!SLIST_EMPTY(&hpriv->roq)) {
|
|
|
|
np = SLIST_FIRST(&hpriv->roq);
|
|
|
|
if (np->seq > seq)
|
|
|
|
break;
|
|
|
|
SLIST_REMOVE_HEAD(&hpriv->roq, next);
|
|
|
|
hpriv->roq_len--;
|
|
|
|
if (last == NULL)
|
|
|
|
SLIST_INSERT_HEAD(&sendq, np, next);
|
|
|
|
else
|
|
|
|
SLIST_INSERT_AFTER(last, np, next);
|
|
|
|
last = np;
|
|
|
|
}
|
2008-03-24 22:55:22 +00:00
|
|
|
|
2018-11-04 19:10:44 +00:00
|
|
|
/*
|
|
|
|
* Pretend we got all packets till the current one
|
|
|
|
* and acknowledge it.
|
|
|
|
*/
|
|
|
|
hpriv->recvSeq = seq;
|
|
|
|
ng_pptpgre_ack(hpriv); /* drops lock */
|
|
|
|
ng_pptpgre_sendq(hpriv, &sendq, &temp);
|
2008-03-24 22:55:22 +00:00
|
|
|
NG_FWD_NEW_DATA(error, item, hpriv->hook, m);
|
2018-11-04 19:10:44 +00:00
|
|
|
ERROUT(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add current (early) packet to the reorder queue. */
|
|
|
|
np->item = item;
|
|
|
|
np->seq = seq;
|
|
|
|
if (prev == NULL)
|
|
|
|
SLIST_INSERT_HEAD(&hpriv->roq, np, next);
|
|
|
|
else
|
|
|
|
SLIST_INSERT_AFTER(prev, np, next);
|
|
|
|
hpriv->roq_len++;
|
|
|
|
|
|
|
|
deliver:
|
|
|
|
/* Look if we have some packets in sequence after sendq. */
|
|
|
|
while (!SLIST_EMPTY(&hpriv->roq)) {
|
|
|
|
np = SLIST_FIRST(&hpriv->roq);
|
|
|
|
if (PPTP_SEQ_DIFF(np->seq, hpriv->recvSeq) > 1)
|
|
|
|
break; /* the gap in the sequence */
|
|
|
|
|
|
|
|
/* "np" is in sequence, move it to the sendq. */
|
|
|
|
SLIST_REMOVE_HEAD(&hpriv->roq, next);
|
|
|
|
hpriv->roq_len--;
|
|
|
|
hpriv->recvSeq = np->seq;
|
|
|
|
|
|
|
|
if (last == NULL)
|
|
|
|
SLIST_INSERT_HEAD(&sendq, np, next);
|
|
|
|
else
|
|
|
|
SLIST_INSERT_AFTER(last, np, next);
|
|
|
|
last = np;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SLIST_EMPTY(&hpriv->roq)) {
|
|
|
|
if (callout_pending(&hpriv->reorderTimer))
|
|
|
|
ng_uncallout(&hpriv->reorderTimer, hpriv->node);
|
2000-05-05 01:11:39 +00:00
|
|
|
} else {
|
2018-11-04 19:10:44 +00:00
|
|
|
if (!callout_pending(&hpriv->reorderTimer))
|
|
|
|
ng_pptpgre_start_reorder_timer(hpriv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SLIST_EMPTY(&sendq)) {
|
|
|
|
/* Current packet has been queued, nothing to free/deliver. */
|
2008-03-24 22:55:22 +00:00
|
|
|
mtx_unlock(&hpriv->mtx);
|
2018-11-04 19:10:44 +00:00
|
|
|
return (error);
|
2000-05-05 01:11:39 +00:00
|
|
|
}
|
2005-09-08 14:26:23 +00:00
|
|
|
|
2018-11-04 19:10:44 +00:00
|
|
|
/* We need to acknowledge last packet; do it soon... */
|
|
|
|
ng_pptpgre_ack(hpriv); /* drops lock */
|
|
|
|
ng_pptpgre_sendq(hpriv, &sendq, &temp);
|
2005-09-08 14:26:23 +00:00
|
|
|
return (error);
|
|
|
|
|
|
|
|
done:
|
|
|
|
NG_FREE_ITEM(item);
|
1999-12-08 18:55:39 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
TIMER RELATED FUNCTIONS
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
/*
|
2000-05-05 01:11:39 +00:00
|
|
|
* Start a timer for the peer's acknowledging our oldest unacknowledged
|
1999-12-08 18:55:39 +00:00
|
|
|
* sequence number. If we get an ack for this sequence number before
|
|
|
|
* the timer goes off, we cancel the timer. Resets currently running
|
|
|
|
* recv ack timer, if any.
|
|
|
|
*/
|
|
|
|
static void
|
2008-03-24 22:55:22 +00:00
|
|
|
ng_pptpgre_start_recv_ack_timer(hpriv_p hpriv)
|
1999-12-08 18:55:39 +00:00
|
|
|
{
|
2000-07-25 00:23:19 +00:00
|
|
|
int remain, ticks;
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* Compute how long until oldest unack'd packet times out,
|
|
|
|
and reset the timer to that time. */
|
2008-03-24 22:55:22 +00:00
|
|
|
remain = (hpriv->timeSent[0] + hpriv->ato) - ng_pptpgre_time();
|
1999-12-08 18:55:39 +00:00
|
|
|
if (remain < 0)
|
|
|
|
remain = 0;
|
2000-05-05 01:11:39 +00:00
|
|
|
|
2002-04-14 17:37:35 +00:00
|
|
|
/* Be conservative: timeout can happen up to 1 tick early */
|
2016-04-26 15:38:17 +00:00
|
|
|
ticks = howmany(remain * hz, PPTP_TIME_SCALE) + 1;
|
2008-03-24 22:55:22 +00:00
|
|
|
ng_callout(&hpriv->rackTimer, hpriv->node, hpriv->hook,
|
|
|
|
ticks, ng_pptpgre_recv_ack_timeout, hpriv, 0);
|
2002-04-14 17:37:35 +00:00
|
|
|
}
|
|
|
|
|
1999-12-08 18:55:39 +00:00
|
|
|
/*
|
|
|
|
* The peer has failed to acknowledge the oldest unacknowledged sequence
|
|
|
|
* number within the time allotted. Update our adaptive timeout parameters
|
|
|
|
* and reset/restart the recv ack timer.
|
|
|
|
*/
|
|
|
|
static void
|
2004-12-09 07:49:02 +00:00
|
|
|
ng_pptpgre_recv_ack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
|
1999-12-08 18:55:39 +00:00
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
2008-03-24 22:55:22 +00:00
|
|
|
const hpriv_p hpriv = arg1;
|
2000-05-05 01:11:39 +00:00
|
|
|
|
1999-12-08 18:55:39 +00:00
|
|
|
/* Update adaptive timeout stuff */
|
2000-05-05 01:11:39 +00:00
|
|
|
priv->stats.recvAckTimeouts++;
|
2008-03-24 22:55:22 +00:00
|
|
|
hpriv->rtt = PPTP_ACK_DELTA(hpriv->rtt) + 1; /* +1 to avoid delta*0 case */
|
|
|
|
hpriv->ato = hpriv->rtt + PPTP_ACK_CHI(hpriv->dev);
|
|
|
|
if (hpriv->ato > PPTP_MAX_TIMEOUT)
|
|
|
|
hpriv->ato = PPTP_MAX_TIMEOUT;
|
|
|
|
else if (hpriv->ato < PPTP_MIN_TIMEOUT)
|
|
|
|
hpriv->ato = PPTP_MIN_TIMEOUT;
|
2000-07-25 00:23:19 +00:00
|
|
|
|
2000-06-28 19:43:34 +00:00
|
|
|
/* Reset ack and sliding window */
|
2008-03-24 22:55:22 +00:00
|
|
|
hpriv->recvAck = hpriv->xmitSeq; /* pretend we got the ack */
|
|
|
|
hpriv->xmitWin = (hpriv->xmitWin + 1) / 2; /* shrink transmit window */
|
|
|
|
hpriv->winAck = hpriv->recvAck + hpriv->xmitWin; /* reset win expand time */
|
1999-12-08 18:55:39 +00:00
|
|
|
}
|
|
|
|
|
2000-05-05 01:11:39 +00:00
|
|
|
/*
|
|
|
|
* Start the send ack timer. This assumes the timer is not
|
|
|
|
* already running.
|
|
|
|
*/
|
|
|
|
static void
|
2008-03-26 21:19:03 +00:00
|
|
|
ng_pptpgre_start_send_ack_timer(hpriv_p hpriv)
|
2000-05-05 01:11:39 +00:00
|
|
|
{
|
2008-03-26 21:19:03 +00:00
|
|
|
int ackTimeout, ticks;
|
|
|
|
|
|
|
|
/* Take 1/4 of the estimated round trip time */
|
|
|
|
ackTimeout = (hpriv->rtt >> 2);
|
|
|
|
if (ackTimeout < PPTP_MIN_ACK_DELAY)
|
|
|
|
ackTimeout = PPTP_MIN_ACK_DELAY;
|
|
|
|
else if (ackTimeout > PPTP_MAX_ACK_DELAY)
|
|
|
|
ackTimeout = PPTP_MAX_ACK_DELAY;
|
2000-05-05 01:11:39 +00:00
|
|
|
|
2002-04-14 17:37:35 +00:00
|
|
|
/* Be conservative: timeout can happen up to 1 tick early */
|
2016-04-26 15:38:17 +00:00
|
|
|
ticks = howmany(ackTimeout * hz, PPTP_TIME_SCALE);
|
2008-03-24 22:55:22 +00:00
|
|
|
ng_callout(&hpriv->sackTimer, hpriv->node, hpriv->hook,
|
|
|
|
ticks, ng_pptpgre_send_ack_timeout, hpriv, 0);
|
2002-04-14 17:37:35 +00:00
|
|
|
}
|
|
|
|
|
1999-12-08 18:55:39 +00:00
|
|
|
/*
|
|
|
|
* We've waited as long as we're willing to wait before sending an
|
|
|
|
* acknowledgement to the peer for received frames. We had hoped to
|
|
|
|
* be able to piggy back our acknowledgement on an outgoing data frame,
|
|
|
|
* but apparently there haven't been any since. So send the ack now.
|
|
|
|
*/
|
|
|
|
static void
|
2004-12-09 07:49:02 +00:00
|
|
|
ng_pptpgre_send_ack_timeout(node_p node, hook_p hook, void *arg1, int arg2)
|
1999-12-08 18:55:39 +00:00
|
|
|
{
|
2008-03-24 22:55:22 +00:00
|
|
|
const hpriv_p hpriv = arg1;
|
2005-09-08 14:26:23 +00:00
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
mtx_lock(&hpriv->mtx);
|
2000-05-05 01:11:39 +00:00
|
|
|
/* Send a frame with an ack but no payload */
|
2008-03-24 22:55:22 +00:00
|
|
|
ng_pptpgre_xmit(hpriv, NULL);
|
|
|
|
mtx_assert(&hpriv->mtx, MA_NOTOWNED);
|
1999-12-08 18:55:39 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 19:10:44 +00:00
|
|
|
/*
|
|
|
|
* Start a timer for the reorder queue. This assumes the timer is not
|
|
|
|
* already running.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ng_pptpgre_start_reorder_timer(hpriv_p hpriv)
|
|
|
|
{
|
|
|
|
int ticks;
|
|
|
|
|
|
|
|
/* Be conservative: timeout can happen up to 1 tick early */
|
|
|
|
ticks = (((reorder_timeout * hz) + 1000 - 1) / 1000) + 1;
|
|
|
|
ng_callout(&hpriv->reorderTimer, hpriv->node, hpriv->hook,
|
|
|
|
ticks, ng_pptpgre_reorder_timeout, hpriv, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The oldest packet spent too much time in the reorder queue.
|
|
|
|
* Deliver it and next packets in sequence, if any.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ng_pptpgre_reorder_timeout(node_p node, hook_p hook, void *arg1, int arg2)
|
|
|
|
{
|
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
|
|
|
const hpriv_p hpriv = arg1;
|
|
|
|
roqh sendq = SLIST_HEAD_INITIALIZER(sendq);
|
|
|
|
struct ng_pptpgre_roq *np, *last = NULL;
|
|
|
|
|
|
|
|
priv->stats.recvReorderTimeouts++;
|
|
|
|
mtx_lock(&hpriv->mtx);
|
|
|
|
if (SLIST_EMPTY(&hpriv->roq)) { /* should not happen */
|
|
|
|
mtx_unlock(&hpriv->mtx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
last = np = SLIST_FIRST(&hpriv->roq);
|
|
|
|
hpriv->roq_len--;
|
|
|
|
SLIST_REMOVE_HEAD(&hpriv->roq, next);
|
|
|
|
SLIST_INSERT_HEAD(&sendq, np, next);
|
|
|
|
|
|
|
|
/* Look if we have more packets in sequence */
|
|
|
|
while (!SLIST_EMPTY(&hpriv->roq)) {
|
|
|
|
np = SLIST_FIRST(&hpriv->roq);
|
|
|
|
if (PPTP_SEQ_DIFF(np->seq, last->seq) > 1)
|
|
|
|
break; /* the gap in the sequence */
|
|
|
|
|
|
|
|
/* Next packet is in sequence, move it to the sendq. */
|
|
|
|
hpriv->roq_len--;
|
|
|
|
SLIST_REMOVE_HEAD(&hpriv->roq, next);
|
|
|
|
SLIST_INSERT_AFTER(last, np, next);
|
|
|
|
last = np;
|
|
|
|
}
|
|
|
|
|
|
|
|
hpriv->recvSeq = last->seq;
|
|
|
|
if (!SLIST_EMPTY(&hpriv->roq))
|
|
|
|
ng_pptpgre_start_reorder_timer(hpriv);
|
|
|
|
|
|
|
|
/* We need to acknowledge last packet; do it soon... */
|
|
|
|
ng_pptpgre_ack(hpriv); /* drops lock */
|
|
|
|
ng_pptpgre_sendq(hpriv, &sendq, NULL);
|
|
|
|
mtx_assert(&hpriv->mtx, MA_NOTOWNED);
|
|
|
|
}
|
|
|
|
|
1999-12-08 18:55:39 +00:00
|
|
|
/*************************************************************************
|
|
|
|
MISC FUNCTIONS
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
/*
|
2008-03-24 22:55:22 +00:00
|
|
|
* Find the hook with a given session ID.
|
1999-12-08 18:55:39 +00:00
|
|
|
*/
|
2008-03-24 22:55:22 +00:00
|
|
|
static hpriv_p
|
|
|
|
ng_pptpgre_find_session(priv_p privp, u_int16_t cid)
|
1999-12-08 18:55:39 +00:00
|
|
|
{
|
2008-03-24 22:55:22 +00:00
|
|
|
uint16_t hash = SESSHASH(cid);
|
|
|
|
hpriv_p hpriv = NULL;
|
1999-12-08 18:55:39 +00:00
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
LIST_FOREACH(hpriv, &privp->sesshash[hash], sessions) {
|
|
|
|
if (hpriv->conf.cid == cid)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (hpriv);
|
|
|
|
}
|
2005-08-30 09:51:54 +00:00
|
|
|
|
2008-03-24 22:55:22 +00:00
|
|
|
/*
|
|
|
|
* Reset state (must be called with lock held or from writer)
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ng_pptpgre_reset(hpriv_p hpriv)
|
|
|
|
{
|
2018-11-04 19:10:44 +00:00
|
|
|
struct ng_pptpgre_roq *np;
|
|
|
|
|
1999-12-08 18:55:39 +00:00
|
|
|
/* Reset adaptive timeout state */
|
2008-03-24 22:55:22 +00:00
|
|
|
hpriv->ato = PPTP_MAX_TIMEOUT;
|
2008-03-26 21:19:03 +00:00
|
|
|
hpriv->rtt = PPTP_TIME_SCALE / 10;
|
|
|
|
if (hpriv->conf.peerPpd > 1) /* ppd = 0 treat as = 1 */
|
|
|
|
hpriv->rtt *= hpriv->conf.peerPpd;
|
2008-03-24 22:55:22 +00:00
|
|
|
hpriv->dev = 0;
|
|
|
|
hpriv->xmitWin = (hpriv->conf.recvWin + 1) / 2;
|
|
|
|
if (hpriv->xmitWin < 2) /* often the first packet is lost */
|
|
|
|
hpriv->xmitWin = 2; /* because the peer isn't ready */
|
|
|
|
else if (hpriv->xmitWin > PPTP_XMIT_WIN)
|
|
|
|
hpriv->xmitWin = PPTP_XMIT_WIN;
|
|
|
|
hpriv->winAck = hpriv->xmitWin;
|
1999-12-08 18:55:39 +00:00
|
|
|
|
|
|
|
/* Reset sequence numbers */
|
2008-03-24 22:55:22 +00:00
|
|
|
hpriv->recvSeq = ~0;
|
|
|
|
hpriv->recvAck = ~0;
|
|
|
|
hpriv->xmitSeq = ~0;
|
|
|
|
hpriv->xmitAck = ~0;
|
1999-12-08 18:55:39 +00:00
|
|
|
|
2002-04-14 17:37:35 +00:00
|
|
|
/* Stop timers */
|
2008-03-26 21:19:03 +00:00
|
|
|
ng_uncallout(&hpriv->sackTimer, hpriv->node);
|
|
|
|
ng_uncallout(&hpriv->rackTimer, hpriv->node);
|
2018-11-04 19:10:44 +00:00
|
|
|
ng_uncallout(&hpriv->reorderTimer, hpriv->node);
|
|
|
|
|
|
|
|
/* Clear reorder queue */
|
|
|
|
while (!SLIST_EMPTY(&hpriv->roq)) {
|
|
|
|
np = SLIST_FIRST(&hpriv->roq);
|
|
|
|
SLIST_REMOVE_HEAD(&hpriv->roq, next);
|
|
|
|
NG_FREE_ITEM(np->item);
|
2024-05-31 16:19:54 +00:00
|
|
|
free(np, M_NETGRAPH_PPTP);
|
2018-11-04 19:10:44 +00:00
|
|
|
}
|
|
|
|
hpriv->roq_len = 0;
|
1999-12-08 18:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the current time scaled & translated to our internally used format.
|
|
|
|
*/
|
|
|
|
static pptptime_t
|
2008-03-24 22:55:22 +00:00
|
|
|
ng_pptpgre_time(void)
|
1999-12-08 18:55:39 +00:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
2000-07-25 00:23:19 +00:00
|
|
|
pptptime_t t;
|
1999-12-08 18:55:39 +00:00
|
|
|
|
2000-07-25 00:23:19 +00:00
|
|
|
microuptime(&tv);
|
|
|
|
t = (pptptime_t)tv.tv_sec * PPTP_TIME_SCALE;
|
2008-03-26 21:19:03 +00:00
|
|
|
t += tv.tv_usec / (1000000 / PPTP_TIME_SCALE);
|
2000-07-25 00:23:19 +00:00
|
|
|
return(t);
|
1999-12-08 18:55:39 +00:00
|
|
|
}
|