#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/time.h>
#include <sys/conf.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/errno.h>
#include <sys/epoch.h>
#include <sys/libkern.h>
#include <net/vnet.h>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include <netgraph/ng_parse.h>
#include <netgraph/ng_l2tp.h>
#ifdef NG_SEPARATE_MALLOC
static MALLOC_DEFINE(M_NETGRAPH_L2TP, "netgraph_l2tp", "netgraph l2tp node");
#else
#define M_NETGRAPH_L2TP M_NETGRAPH
#endif
#define L2TP_HDR_CTRL 0x8000
#define L2TP_HDR_LEN 0x4000
#define L2TP_HDR_SEQ 0x0800
#define L2TP_HDR_OFF 0x0200
#define L2TP_HDR_PRIO 0x0100
#define L2TP_HDR_VERS_MASK 0x000f
#define L2TP_HDR_VERSION 0x0002
#define L2TP_CTRL_0BITS 0x030d
#define L2TP_CTRL_1BITS 0xc802
#define L2TP_DATA_0BITS 0x800d
#define L2TP_DATA_1BITS 0x0002
#define L2TP_CTRL_HDR (L2TP_HDR_CTRL | L2TP_HDR_LEN \
| L2TP_HDR_SEQ | L2TP_HDR_VERSION)
#define L2TP_DATA_HDR (L2TP_HDR_VERSION)
#define L2TP_MAX_XWIN 128
#define L2TP_MAX_REXMIT 5
#define L2TP_MAX_REXMIT_TO 30
#define L2TP_DELAYED_ACK ((hz + 19) / 20)
#define L2TP_CONTROL_DSEQ 1
#define L2TP_ENABLE_DSEQ 1
#define L2TP_SEQ_DIFF(x, y) ((int16_t)((x) - (y)))
#define SESSHASHSIZE 0x0020
#define SESSHASH(x) (((x) ^ ((x) >> 8)) & (SESSHASHSIZE - 1))
struct ng_l2tp_hook_private {
struct ng_l2tp_sess_config conf;
struct ng_l2tp_session_stats stats;
hook_p hook;
u_int16_t ns;
u_int16_t nr;
LIST_ENTRY(ng_l2tp_hook_private) sessions;
};
typedef struct ng_l2tp_hook_private *hookpriv_p;
struct l2tp_seq {
u_int16_t ns;
u_int16_t nr;
u_int16_t inproc;
u_int16_t rack;
u_int16_t xack;
u_int16_t wmax;
u_int16_t cwnd;
u_int16_t ssth;
u_int16_t acks;
u_int16_t rexmits;
struct callout rack_timer;
struct callout xack_timer;
struct mbuf *xwin[L2TP_MAX_XWIN];
struct mtx mtx;
};
struct ng_l2tp_private {
node_p node;
hook_p ctrl;
hook_p lower;
struct ng_l2tp_config conf;
struct ng_l2tp_stats stats;
struct l2tp_seq seq;
ng_ID_t ftarget;
LIST_HEAD(, ng_l2tp_hook_private) sesshash[SESSHASHSIZE];
};
typedef struct ng_l2tp_private *priv_p;
static ng_constructor_t ng_l2tp_constructor;
static ng_rcvmsg_t ng_l2tp_rcvmsg;
static ng_shutdown_t ng_l2tp_shutdown;
static ng_newhook_t ng_l2tp_newhook;
static ng_rcvdata_t ng_l2tp_rcvdata;
static ng_rcvdata_t ng_l2tp_rcvdata_lower;
static ng_rcvdata_t ng_l2tp_rcvdata_ctrl;
static ng_disconnect_t ng_l2tp_disconnect;
static int ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns);
static void ng_l2tp_seq_init(priv_p priv);
static int ng_l2tp_seq_set(priv_p priv,
const struct ng_l2tp_seq_config *conf);
static int ng_l2tp_seq_adjust(priv_p priv,
const struct ng_l2tp_config *conf);
static void ng_l2tp_seq_reset(priv_p priv);
static void ng_l2tp_seq_failure(priv_p priv);
static void ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr);
static void ng_l2tp_seq_xack_timeout(void *);
static void ng_l2tp_seq_rack_timeout(void *);
static hookpriv_p ng_l2tp_find_session(priv_p privp, u_int16_t sid);
static ng_fn_eachhook ng_l2tp_reset_session;
static const struct ng_parse_struct_field
ng_l2tp_seq_config_fields[] = NG_L2TP_SEQ_CONFIG_TYPE_INFO;
static const struct ng_parse_type ng_l2tp_seq_config_type = {
&ng_parse_struct_type,
&ng_l2tp_seq_config_fields
};
static const struct ng_parse_struct_field
ng_l2tp_config_type_fields[] = NG_L2TP_CONFIG_TYPE_INFO;
static const struct ng_parse_type ng_l2tp_config_type = {
&ng_parse_struct_type,
&ng_l2tp_config_type_fields,
};
static const struct ng_parse_struct_field
ng_l2tp_sess_config_type_fields[] = NG_L2TP_SESS_CONFIG_TYPE_INFO;
static const struct ng_parse_type ng_l2tp_sess_config_type = {
&ng_parse_struct_type,
&ng_l2tp_sess_config_type_fields,
};
static const struct ng_parse_struct_field
ng_l2tp_stats_type_fields[] = NG_L2TP_STATS_TYPE_INFO;
static const struct ng_parse_type ng_l2tp_stats_type = {
&ng_parse_struct_type,
&ng_l2tp_stats_type_fields
};
static const struct ng_parse_struct_field
ng_l2tp_session_stats_type_fields[] = NG_L2TP_SESSION_STATS_TYPE_INFO;
static const struct ng_parse_type ng_l2tp_session_stats_type = {
&ng_parse_struct_type,
&ng_l2tp_session_stats_type_fields
};
static const struct ng_cmdlist ng_l2tp_cmdlist[] = {
{
NGM_L2TP_COOKIE,
NGM_L2TP_SET_CONFIG,
"setconfig",
&ng_l2tp_config_type,
NULL
},
{
NGM_L2TP_COOKIE,
NGM_L2TP_GET_CONFIG,
"getconfig",
NULL,
&ng_l2tp_config_type
},
{
NGM_L2TP_COOKIE,
NGM_L2TP_SET_SESS_CONFIG,
"setsessconfig",
&ng_l2tp_sess_config_type,
NULL
},
{
NGM_L2TP_COOKIE,
NGM_L2TP_GET_SESS_CONFIG,
"getsessconfig",
&ng_parse_hint16_type,
&ng_l2tp_sess_config_type
},
{
NGM_L2TP_COOKIE,
NGM_L2TP_GET_STATS,
"getstats",
NULL,
&ng_l2tp_stats_type
},
{
NGM_L2TP_COOKIE,
NGM_L2TP_CLR_STATS,
"clrstats",
NULL,
NULL
},
{
NGM_L2TP_COOKIE,
NGM_L2TP_GETCLR_STATS,
"getclrstats",
NULL,
&ng_l2tp_stats_type
},
{
NGM_L2TP_COOKIE,
NGM_L2TP_GET_SESSION_STATS,
"getsessstats",
&ng_parse_int16_type,
&ng_l2tp_session_stats_type
},
{
NGM_L2TP_COOKIE,
NGM_L2TP_CLR_SESSION_STATS,
"clrsessstats",
&ng_parse_int16_type,
NULL
},
{
NGM_L2TP_COOKIE,
NGM_L2TP_GETCLR_SESSION_STATS,
"getclrsessstats",
&ng_parse_int16_type,
&ng_l2tp_session_stats_type
},
{
NGM_L2TP_COOKIE,
NGM_L2TP_ACK_FAILURE,
"ackfailure",
NULL,
NULL
},
{
NGM_L2TP_COOKIE,
NGM_L2TP_SET_SEQ,
"setsequence",
&ng_l2tp_seq_config_type,
NULL
},
{ 0 }
};
static struct ng_type ng_l2tp_typestruct = {
.version = NG_ABI_VERSION,
.name = NG_L2TP_NODE_TYPE,
.constructor = ng_l2tp_constructor,
.rcvmsg = ng_l2tp_rcvmsg,
.shutdown = ng_l2tp_shutdown,
.newhook = ng_l2tp_newhook,
.rcvdata = ng_l2tp_rcvdata,
.disconnect = ng_l2tp_disconnect,
.cmdlist = ng_l2tp_cmdlist,
};
NETGRAPH_INIT(l2tp, &ng_l2tp_typestruct);
#ifdef INVARIANTS
static void ng_l2tp_seq_check(struct l2tp_seq *seq);
#define SEQ_LOCK(seq) do { \
mtx_lock(&(seq)->mtx); \
ng_l2tp_seq_check(seq); \
} while (0)
#define SEQ_UNLOCK(seq) do { \
ng_l2tp_seq_check(seq); \
mtx_unlock(&(seq)->mtx); \
} while (0)
#else
#define SEQ_LOCK(seq) mtx_lock(&(seq)->mtx)
#define SEQ_UNLOCK(seq) mtx_unlock(&(seq)->mtx)
#endif
#define SEQ_LOCK_ASSERT(seq) mtx_assert(&(seq)->mtx, MA_OWNED)
#define L2TP_COPY_MBUF m_copypacket
#define ERROUT(x) do { error = (x); goto done; } while (0)
static int
ng_l2tp_constructor(node_p node)
{
priv_p priv;
int i;
priv = malloc(sizeof(*priv), M_NETGRAPH_L2TP, M_WAITOK | M_ZERO);
NG_NODE_SET_PRIVATE(node, priv);
priv->node = node;
priv->conf.peer_win = 1;
priv->conf.rexmit_max = L2TP_MAX_REXMIT;
priv->conf.rexmit_max_to = L2TP_MAX_REXMIT_TO;
ng_l2tp_seq_init(priv);
for (i = 0; i < SESSHASHSIZE; i++)
LIST_INIT(&priv->sesshash[i]);
return (0);
}
static int
ng_l2tp_newhook(node_p node, hook_p hook, const char *name)
{
const priv_p priv = NG_NODE_PRIVATE(node);
if (strcmp(name, NG_L2TP_HOOK_CTRL) == 0) {
if (priv->ctrl != NULL)
return (EISCONN);
priv->ctrl = hook;
NG_HOOK_SET_RCVDATA(hook, ng_l2tp_rcvdata_ctrl);
} else if (strcmp(name, NG_L2TP_HOOK_LOWER) == 0) {
if (priv->lower != NULL)
return (EISCONN);
priv->lower = hook;
NG_HOOK_SET_RCVDATA(hook, ng_l2tp_rcvdata_lower);
} else {
static const char hexdig[16] = "0123456789abcdef";
u_int16_t session_id;
hookpriv_p hpriv;
uint16_t hash;
const char *hex;
int i;
int j;
if (strncmp(name, NG_L2TP_HOOK_SESSION_P,
sizeof(NG_L2TP_HOOK_SESSION_P) - 1) != 0)
return (EINVAL);
hex = name + sizeof(NG_L2TP_HOOK_SESSION_P) - 1;
for (session_id = i = 0; i < 4; i++) {
for (j = 0; j < 16 && hex[i] != hexdig[j]; j++);
if (j == 16)
return (EINVAL);
session_id = (session_id << 4) | j;
}
if (hex[i] != '\0')
return (EINVAL);
hpriv = malloc(sizeof(*hpriv),
M_NETGRAPH_L2TP, M_NOWAIT | M_ZERO);
if (hpriv == NULL)
return (ENOMEM);
hpriv->conf.session_id = session_id;
hpriv->conf.control_dseq = L2TP_CONTROL_DSEQ;
hpriv->conf.enable_dseq = L2TP_ENABLE_DSEQ;
hpriv->hook = hook;
NG_HOOK_SET_PRIVATE(hook, hpriv);
hash = SESSHASH(hpriv->conf.session_id);
LIST_INSERT_HEAD(&priv->sesshash[hash], hpriv, sessions);
}
return (0);
}
static int
ng_l2tp_rcvmsg(node_p node, item_p item, hook_p lasthook)
{
const priv_p priv = NG_NODE_PRIVATE(node);
struct ng_mesg *resp = NULL;
struct ng_mesg *msg;
int error = 0;
NGI_GET_MSG(item, msg);
switch (msg->header.typecookie) {
case NGM_L2TP_COOKIE:
switch (msg->header.cmd) {
case NGM_L2TP_SET_CONFIG:
{
struct ng_l2tp_config *const conf =
(struct ng_l2tp_config *)msg->data;
if (msg->header.arglen != sizeof(*conf)) {
error = EINVAL;
break;
}
conf->enabled = !!conf->enabled;
conf->match_id = !!conf->match_id;
if (priv->conf.enabled
&& ((priv->conf.tunnel_id != 0
&& conf->tunnel_id != priv->conf.tunnel_id)
|| ((priv->conf.peer_id != 0
&& conf->peer_id != priv->conf.peer_id)))) {
error = EBUSY;
break;
}
priv->ftarget = NGI_RETADDR(item);
if ((error = ng_l2tp_seq_adjust(priv, conf)) != 0)
break;
priv->conf = *conf;
break;
}
case NGM_L2TP_GET_CONFIG:
{
struct ng_l2tp_config *conf;
NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
if (resp == NULL) {
error = ENOMEM;
break;
}
conf = (struct ng_l2tp_config *)resp->data;
*conf = priv->conf;
break;
}
case NGM_L2TP_SET_SESS_CONFIG:
{
struct ng_l2tp_sess_config *const conf =
(struct ng_l2tp_sess_config *)msg->data;
hookpriv_p hpriv;
if (msg->header.arglen != sizeof(*conf)) {
error = EINVAL;
break;
}
hpriv = ng_l2tp_find_session(priv, conf->session_id);
if (hpriv == NULL) {
error = ENOENT;
break;
}
hpriv->conf = *conf;
break;
}
case NGM_L2TP_GET_SESS_CONFIG:
{
struct ng_l2tp_sess_config *conf;
u_int16_t session_id;
hookpriv_p hpriv;
if (msg->header.arglen != sizeof(session_id)) {
error = EINVAL;
break;
}
memcpy(&session_id, msg->data, 2);
hpriv = ng_l2tp_find_session(priv, session_id);
if (hpriv == NULL) {
error = ENOENT;
break;
}
NG_MKRESPONSE(resp, msg, sizeof(hpriv->conf), M_NOWAIT);
if (resp == NULL) {
error = ENOMEM;
break;
}
conf = (struct ng_l2tp_sess_config *)resp->data;
*conf = hpriv->conf;
break;
}
case NGM_L2TP_GET_STATS:
case NGM_L2TP_CLR_STATS:
case NGM_L2TP_GETCLR_STATS:
{
if (msg->header.cmd != NGM_L2TP_CLR_STATS) {
NG_MKRESPONSE(resp, msg,
sizeof(priv->stats), M_NOWAIT);
if (resp == NULL) {
error = ENOMEM;
break;
}
memcpy(resp->data,
&priv->stats, sizeof(priv->stats));
}
if (msg->header.cmd != NGM_L2TP_GET_STATS)
memset(&priv->stats, 0, sizeof(priv->stats));
break;
}
case NGM_L2TP_GET_SESSION_STATS:
case NGM_L2TP_CLR_SESSION_STATS:
case NGM_L2TP_GETCLR_SESSION_STATS:
{
uint16_t session_id;
hookpriv_p hpriv;
if (msg->header.arglen != sizeof(session_id)) {
error = EINVAL;
break;
}
bcopy(msg->data, &session_id, sizeof(uint16_t));
hpriv = ng_l2tp_find_session(priv, session_id);
if (hpriv == NULL) {
error = ENOENT;
break;
}
if (msg->header.cmd != NGM_L2TP_CLR_SESSION_STATS) {
NG_MKRESPONSE(resp, msg,
sizeof(hpriv->stats), M_NOWAIT);
if (resp == NULL) {
error = ENOMEM;
break;
}
bcopy(&hpriv->stats, resp->data,
sizeof(hpriv->stats));
}
if (msg->header.cmd != NGM_L2TP_GET_SESSION_STATS)
bzero(&hpriv->stats, sizeof(hpriv->stats));
break;
}
case NGM_L2TP_SET_SEQ:
{
struct ng_l2tp_seq_config *const conf =
(struct ng_l2tp_seq_config *)msg->data;
if (msg->header.arglen != sizeof(*conf)) {
error = EINVAL;
break;
}
conf->ns = htons(conf->ns);
conf->nr = htons(conf->nr);
conf->rack = htons(conf->rack);
conf->xack = htons(conf->xack);
error = ng_l2tp_seq_set(priv, conf);
break;
}
default:
error = EINVAL;
break;
}
break;
default:
error = EINVAL;
break;
}
NG_RESPOND_MSG(error, node, item, resp);
NG_FREE_MSG(msg);
return (error);
}
static int
ng_l2tp_shutdown(node_p node)
{
const priv_p priv = NG_NODE_PRIVATE(node);
struct l2tp_seq *const seq = &priv->seq;
SEQ_LOCK(seq);
ng_l2tp_seq_reset(priv);
SEQ_UNLOCK(seq);
callout_drain(&seq->rack_timer);
callout_drain(&seq->xack_timer);
mtx_destroy(&seq->mtx);
free(priv, M_NETGRAPH_L2TP);
NG_NODE_UNREF(node);
return (0);
}
static int
ng_l2tp_disconnect(hook_p hook)
{
const node_p node = NG_HOOK_NODE(hook);
const priv_p priv = NG_NODE_PRIVATE(node);
if (hook == priv->ctrl)
priv->ctrl = NULL;
else if (hook == priv->lower)
priv->lower = NULL;
else {
const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
LIST_REMOVE(hpriv, sessions);
free(hpriv, M_NETGRAPH_L2TP);
NG_HOOK_SET_PRIVATE(hook, NULL);
}
if (NG_NODE_NUMHOOKS(node) == 0 && NG_NODE_IS_VALID(node))
ng_rmnode_self(node);
return (0);
}
static hookpriv_p
ng_l2tp_find_session(priv_p privp, u_int16_t sid)
{
uint16_t hash = SESSHASH(sid);
hookpriv_p hpriv = NULL;
LIST_FOREACH(hpriv, &privp->sesshash[hash], sessions) {
if (hpriv->conf.session_id == sid)
break;
}
return (hpriv);
}
static int
ng_l2tp_reset_session(hook_p hook, void *arg)
{
const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
if (hpriv != NULL) {
hpriv->conf.control_dseq = 0;
hpriv->conf.enable_dseq = 0;
bzero(&hpriv->stats, sizeof(struct ng_l2tp_session_stats));
hpriv->nr = 0;
hpriv->ns = 0;
}
return (-1);
}
static int
ng_l2tp_rcvdata_lower(hook_p h, item_p item)
{
static const u_int16_t req_bits[2][2] = {
{ L2TP_DATA_0BITS, L2TP_DATA_1BITS },
{ L2TP_CTRL_0BITS, L2TP_CTRL_1BITS },
};
const node_p node = NG_HOOK_NODE(h);
const priv_p priv = NG_NODE_PRIVATE(node);
hookpriv_p hpriv = NULL;
hook_p hook = NULL;
struct mbuf *m;
u_int16_t tid, sid;
u_int16_t hdr;
u_int16_t ns, nr;
int is_ctrl;
int error;
int len, plen;
if (!priv->conf.enabled) {
NG_FREE_ITEM(item);
ERROUT(ENXIO);
}
NGI_GET_M(item, m);
plen = m->m_pkthdr.len;
priv->stats.recvPackets++;
priv->stats.recvOctets += plen;
if (m->m_pkthdr.len < 6) {
priv->stats.recvRunts++;
NG_FREE_ITEM(item);
NG_FREE_M(m);
ERROUT(EINVAL);
}
if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
priv->stats.memoryFailures++;
NG_FREE_ITEM(item);
ERROUT(EINVAL);
}
hdr = (mtod(m, uint8_t *)[0] << 8) + mtod(m, uint8_t *)[1];
m_adj(m, 2);
is_ctrl = (hdr & L2TP_HDR_CTRL) != 0;
if ((hdr & req_bits[is_ctrl][0]) != 0
|| (~hdr & req_bits[is_ctrl][1]) != 0) {
priv->stats.recvInvalid++;
NG_FREE_ITEM(item);
NG_FREE_M(m);
ERROUT(EINVAL);
}
if (m->m_pkthdr.len < 4
+ (2 * ((hdr & L2TP_HDR_LEN) != 0))
+ (4 * ((hdr & L2TP_HDR_SEQ) != 0))
+ (2 * ((hdr & L2TP_HDR_OFF) != 0))) {
priv->stats.recvRunts++;
NG_FREE_ITEM(item);
NG_FREE_M(m);
ERROUT(EINVAL);
}
if ((hdr & L2TP_HDR_LEN) != 0) {
if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
priv->stats.memoryFailures++;
NG_FREE_ITEM(item);
ERROUT(EINVAL);
}
len = (mtod(m, uint8_t *)[0] << 8) + mtod(m, uint8_t *)[1] - 4;
m_adj(m, 2);
if (len < 0 || len > m->m_pkthdr.len) {
priv->stats.recvInvalid++;
NG_FREE_ITEM(item);
NG_FREE_M(m);
ERROUT(EINVAL);
}
if (len < m->m_pkthdr.len)
m_adj(m, -(m->m_pkthdr.len - len));
}
if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
priv->stats.memoryFailures++;
NG_FREE_ITEM(item);
ERROUT(EINVAL);
}
tid = (mtod(m, u_int8_t *)[0] << 8) + mtod(m, u_int8_t *)[1];
sid = (mtod(m, u_int8_t *)[2] << 8) + mtod(m, u_int8_t *)[3];
m_adj(m, 4);
if (tid != priv->conf.tunnel_id &&
(priv->conf.match_id || tid != 0)) {
priv->stats.recvWrongTunnel++;
NG_FREE_ITEM(item);
NG_FREE_M(m);
ERROUT(EADDRNOTAVAIL);
}
if ((hdr & L2TP_HDR_CTRL) == 0) {
hpriv = ng_l2tp_find_session(priv, sid);
if (hpriv == NULL) {
priv->stats.recvUnknownSID++;
NG_FREE_ITEM(item);
NG_FREE_M(m);
ERROUT(ENOTCONN);
}
hook = hpriv->hook;
}
if ((hdr & L2TP_HDR_SEQ) != 0) {
if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
priv->stats.memoryFailures++;
NG_FREE_ITEM(item);
ERROUT(EINVAL);
}
ns = (mtod(m, u_int8_t *)[0] << 8) + mtod(m, u_int8_t *)[1];
nr = (mtod(m, u_int8_t *)[2] << 8) + mtod(m, u_int8_t *)[3];
m_adj(m, 4);
} else
ns = nr = 0;
if ((hdr & L2TP_HDR_OFF) != 0) {
u_int16_t offset;
if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
priv->stats.memoryFailures++;
NG_FREE_ITEM(item);
ERROUT(EINVAL);
}
offset = (mtod(m, u_int8_t *)[0] << 8) + mtod(m, u_int8_t *)[1];
if ((2+offset) > m->m_pkthdr.len) {
priv->stats.recvInvalid++;
NG_FREE_ITEM(item);
NG_FREE_M(m);
ERROUT(EINVAL);
}
m_adj(m, 2+offset);
}
if ((hdr & L2TP_HDR_CTRL) != 0) {
struct l2tp_seq *const seq = &priv->seq;
SEQ_LOCK(seq);
ng_l2tp_seq_recv_nr(priv, nr);
if (m->m_pkthdr.len == 0) {
SEQ_UNLOCK(seq);
priv->stats.recvZLBs++;
NG_FREE_ITEM(item);
NG_FREE_M(m);
ERROUT(0);
}
if (ns != seq->nr || seq->inproc) {
if (L2TP_SEQ_DIFF(ns, seq->nr) <= 0)
priv->stats.recvDuplicates++;
else
priv->stats.recvOutOfOrder++;
ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
NG_FREE_ITEM(item);
NG_FREE_M(m);
ERROUT(0);
}
M_PREPEND(m, 2, M_NOWAIT);
if (m == NULL) {
SEQ_UNLOCK(seq);
priv->stats.memoryFailures++;
NG_FREE_ITEM(item);
ERROUT(ENOBUFS);
}
mtod(m, u_int8_t *)[0] = sid >> 8;
mtod(m, u_int8_t *)[1] = sid & 0xff;
seq->inproc = 1;
SEQ_UNLOCK(seq);
NG_FWD_NEW_DATA(error, item, priv->ctrl, m);
SEQ_LOCK(seq);
seq->inproc = 0;
if (error == 0) {
seq->nr++;
if (!callout_active(&seq->xack_timer)) {
callout_reset(&seq->xack_timer,
L2TP_DELAYED_ACK, ng_l2tp_seq_xack_timeout,
node);
}
}
SEQ_UNLOCK(seq);
ERROUT(error);
}
hpriv->stats.recvPackets++;
hpriv->stats.recvOctets += plen;
if (!hpriv->conf.control_dseq)
hpriv->conf.enable_dseq = ((hdr & L2TP_HDR_SEQ) != 0);
if ((hdr & L2TP_HDR_SEQ) != 0) {
if (hpriv->conf.enable_dseq
&& L2TP_SEQ_DIFF(ns, hpriv->nr) < 0) {
NG_FREE_ITEM(item);
NG_FREE_M(m);
priv->stats.recvDataDrops++;
ERROUT(0);
}
hpriv->nr = ns + 1;
}
if (m->m_pkthdr.len == 0) {
NG_FREE_ITEM(item);
NG_FREE_M(m);
ERROUT(0);
}
NG_FWD_NEW_DATA(error, item, hook, m);
done:
return (error);
}
static int
ng_l2tp_rcvdata_ctrl(hook_p hook, item_p item)
{
const node_p node = NG_HOOK_NODE(hook);
const priv_p priv = NG_NODE_PRIVATE(node);
struct l2tp_seq *const seq = &priv->seq;
struct mbuf *m;
int error;
int i;
u_int16_t ns;
if (!priv->conf.enabled) {
NG_FREE_ITEM(item);
ERROUT(ENXIO);
}
NGI_GET_M(item, m);
NG_FREE_ITEM(item);
if (m->m_pkthdr.len < 2) {
priv->stats.xmitInvalid++;
m_freem(m);
ERROUT(EINVAL);
}
if (m->m_pkthdr.len >= 0x10000 - 14) {
priv->stats.xmitTooBig++;
m_freem(m);
ERROUT(EOVERFLOW);
}
SEQ_LOCK(seq);
for (i = 0; i < L2TP_MAX_XWIN && seq->xwin[i] != NULL; i++);
if (i == L2TP_MAX_XWIN) {
SEQ_UNLOCK(seq);
priv->stats.xmitDrops++;
m_freem(m);
ERROUT(ENOBUFS);
}
seq->xwin[i] = m;
if (i >= seq->cwnd) {
SEQ_UNLOCK(seq);
ERROUT(0);
}
if (!callout_active(&seq->rack_timer))
callout_reset(&seq->rack_timer, hz, ng_l2tp_seq_rack_timeout,
node);
ns = seq->ns++;
if ((m = L2TP_COPY_MBUF(m, M_NOWAIT)) == NULL) {
SEQ_UNLOCK(seq);
priv->stats.memoryFailures++;
ERROUT(ENOBUFS);
}
error = ng_l2tp_xmit_ctrl(priv, m, ns);
done:
return (error);
}
static int
ng_l2tp_rcvdata(hook_p hook, item_p item)
{
const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
const hookpriv_p hpriv = NG_HOOK_PRIVATE(hook);
struct mbuf *m;
uint8_t *p;
u_int16_t hdr;
int error;
int i = 2;
if (!priv->conf.enabled) {
NG_FREE_ITEM(item);
ERROUT(ENXIO);
}
NGI_GET_M(item, m);
if (m->m_pkthdr.len >= 0x10000 - 12) {
priv->stats.xmitDataTooBig++;
NG_FREE_ITEM(item);
NG_FREE_M(m);
ERROUT(EOVERFLOW);
}
M_PREPEND(m, 6
+ (2 * (hpriv->conf.include_length != 0))
+ (4 * (hpriv->conf.enable_dseq != 0)),
M_NOWAIT);
if (m == NULL) {
priv->stats.memoryFailures++;
NG_FREE_ITEM(item);
ERROUT(ENOBUFS);
}
p = mtod(m, uint8_t *);
hdr = L2TP_DATA_HDR;
if (hpriv->conf.include_length) {
hdr |= L2TP_HDR_LEN;
p[i++] = m->m_pkthdr.len >> 8;
p[i++] = m->m_pkthdr.len & 0xff;
}
p[i++] = priv->conf.peer_id >> 8;
p[i++] = priv->conf.peer_id & 0xff;
p[i++] = hpriv->conf.peer_id >> 8;
p[i++] = hpriv->conf.peer_id & 0xff;
if (hpriv->conf.enable_dseq) {
hdr |= L2TP_HDR_SEQ;
p[i++] = hpriv->ns >> 8;
p[i++] = hpriv->ns & 0xff;
p[i++] = hpriv->nr >> 8;
p[i++] = hpriv->nr & 0xff;
hpriv->ns++;
}
p[0] = hdr >> 8;
p[1] = hdr & 0xff;
hpriv->stats.xmitPackets++;
hpriv->stats.xmitOctets += m->m_pkthdr.len;
priv->stats.xmitPackets++;
priv->stats.xmitOctets += m->m_pkthdr.len;
NG_FWD_NEW_DATA(error, item, priv->lower, m);
done:
return (error);
}
static void
ng_l2tp_seq_failure(priv_p priv)
{
struct ng_mesg *msg;
int error;
NG_MKMESSAGE(msg, NGM_L2TP_COOKIE, NGM_L2TP_ACK_FAILURE, 0, M_NOWAIT);
if (msg == NULL)
return;
NG_SEND_MSG_ID(error, priv->node, msg, priv->ftarget, 0);
}
static void
ng_l2tp_seq_init(priv_p priv)
{
struct l2tp_seq *const seq = &priv->seq;
KASSERT(priv->conf.peer_win >= 1,
("%s: peer_win is zero", __func__));
memset(seq, 0, sizeof(*seq));
seq->cwnd = 1;
seq->wmax = priv->conf.peer_win;
if (seq->wmax > L2TP_MAX_XWIN)
seq->wmax = L2TP_MAX_XWIN;
seq->ssth = seq->wmax;
mtx_init(&seq->mtx, "ng_l2tp", NULL, MTX_DEF);
callout_init_mtx(&seq->rack_timer, &seq->mtx, CALLOUT_RETURNUNLOCKED);
callout_init_mtx(&seq->xack_timer, &seq->mtx, CALLOUT_RETURNUNLOCKED);
}
static int
ng_l2tp_seq_set(priv_p priv, const struct ng_l2tp_seq_config *conf)
{
struct l2tp_seq *const seq = &priv->seq;
if (priv->conf.enabled)
return (EBUSY);
if (conf->xack != conf->nr || conf->ns != conf->rack)
return (EINVAL);
seq->ns = conf->ns;
seq->nr = conf->nr;
seq->rack = conf->rack;
seq->xack = conf->xack;
return (0);
}
static int
ng_l2tp_seq_adjust(priv_p priv, const struct ng_l2tp_config *conf)
{
struct l2tp_seq *const seq = &priv->seq;
u_int16_t new_wmax;
int error = 0;
SEQ_LOCK(seq);
if (!conf->enabled) {
ng_l2tp_seq_reset(priv);
SEQ_UNLOCK(seq);
return (0);
}
new_wmax = conf->peer_win;
if (new_wmax > L2TP_MAX_XWIN)
new_wmax = L2TP_MAX_XWIN;
if (new_wmax == 0)
ERROUT(EINVAL);
if (new_wmax < seq->wmax)
ERROUT(EBUSY);
seq->wmax = new_wmax;
done:
SEQ_UNLOCK(seq);
return (error);
}
static void
ng_l2tp_seq_reset(priv_p priv)
{
struct l2tp_seq *const seq = &priv->seq;
int i;
SEQ_LOCK_ASSERT(seq);
(void )callout_stop(&seq->rack_timer);
(void )callout_stop(&seq->xack_timer);
for (i = 0; i < L2TP_MAX_XWIN; i++) {
if (seq->xwin[i] == NULL)
break;
m_freem(seq->xwin[i]);
}
NG_NODE_FOREACH_HOOK(priv->node, ng_l2tp_reset_session, NULL);
seq->ns = 0;
seq->nr = 0;
seq->rack = 0;
seq->xack = 0;
seq->wmax = L2TP_MAX_XWIN;
seq->cwnd = 1;
seq->ssth = seq->wmax;
seq->acks = 0;
seq->rexmits = 0;
bzero(seq->xwin, sizeof(seq->xwin));
}
static void
ng_l2tp_seq_recv_nr(priv_p priv, u_int16_t nr)
{
struct l2tp_seq *const seq = &priv->seq;
struct mbuf *xwin[L2TP_MAX_XWIN];
int nack;
int i, j;
uint16_t ns;
SEQ_LOCK_ASSERT(seq);
if ((nack = L2TP_SEQ_DIFF(nr, seq->rack)) <= 0)
return;
if (L2TP_SEQ_DIFF(nr, seq->ns) > 0) {
priv->stats.recvBadAcks++;
return;
}
KASSERT(nack <= L2TP_MAX_XWIN,
("%s: nack=%d > %d", __func__, nack, L2TP_MAX_XWIN));
seq->rack = nr;
seq->rexmits = 0;
for (i = 0; i < nack; i++)
m_freem(seq->xwin[i]);
memmove(seq->xwin, seq->xwin + nack,
(L2TP_MAX_XWIN - nack) * sizeof(*seq->xwin));
memset(seq->xwin + (L2TP_MAX_XWIN - nack), 0,
nack * sizeof(*seq->xwin));
if (seq->cwnd < seq->wmax) {
if (seq->cwnd < seq->ssth) {
seq->cwnd += nack;
nack = 0;
if (seq->cwnd > seq->ssth) {
nack = seq->cwnd - seq->ssth;
seq->cwnd = seq->ssth;
}
}
if (seq->cwnd >= seq->ssth) {
seq->acks += nack;
while (seq->acks >= seq->cwnd) {
seq->acks -= seq->cwnd;
if (seq->cwnd < seq->wmax)
seq->cwnd++;
}
}
}
if (callout_active(&seq->rack_timer))
(void )callout_stop(&seq->rack_timer);
if (seq->xwin[0] == NULL)
return;
callout_reset(&seq->rack_timer, hz, ng_l2tp_seq_rack_timeout,
priv->node);
ns = seq->ns;
j = 0;
while ((i = L2TP_SEQ_DIFF(seq->ns, seq->rack)) < seq->cwnd
&& seq->xwin[i] != NULL) {
xwin[j++] = seq->xwin[i];
seq->ns++;
}
for (i = 0; i < j; i++) {
struct mbuf *m;
if ((m = L2TP_COPY_MBUF(xwin[i], M_NOWAIT)) == NULL)
priv->stats.memoryFailures++;
else {
ng_l2tp_xmit_ctrl(priv, m, ns);
SEQ_LOCK(seq);
}
ns++;
}
}
static void
ng_l2tp_seq_xack_timeout(void *arg)
{
const node_p node = arg;
const priv_p priv = NG_NODE_PRIVATE(node);
struct epoch_tracker et;
struct l2tp_seq *const seq = &priv->seq;
SEQ_LOCK_ASSERT(seq);
MPASS(!callout_pending(&seq->xack_timer));
MPASS(callout_active(&seq->xack_timer));
NET_EPOCH_ENTER(et);
CURVNET_SET(node->nd_vnet);
ng_l2tp_xmit_ctrl(priv, NULL, seq->ns);
CURVNET_RESTORE();
NET_EPOCH_EXIT(et);
}
static void
ng_l2tp_seq_rack_timeout(void *arg)
{
const node_p node = arg;
const priv_p priv = NG_NODE_PRIVATE(node);
struct epoch_tracker et;
struct l2tp_seq *const seq = &priv->seq;
struct mbuf *m;
u_int delay;
SEQ_LOCK_ASSERT(seq);
MPASS(seq->xwin[0]);
MPASS(!callout_pending(&seq->rack_timer));
MPASS(callout_active(&seq->rack_timer));
NET_EPOCH_ENTER(et);
CURVNET_SET(node->nd_vnet);
priv->stats.xmitRetransmits++;
if (seq->rexmits++ >= priv->conf.rexmit_max)
ng_l2tp_seq_failure(priv);
delay = (seq->rexmits > 12) ? (1 << 12) : (1 << seq->rexmits);
if (delay > priv->conf.rexmit_max_to)
delay = priv->conf.rexmit_max_to;
callout_reset(&seq->rack_timer, hz * delay, ng_l2tp_seq_rack_timeout,
node);
seq->ns = seq->rack;
seq->ssth = (seq->cwnd + 1) / 2;
seq->cwnd = 1;
seq->acks = 0;
m = L2TP_COPY_MBUF(seq->xwin[0], M_NOWAIT);
if (m == NULL) {
SEQ_UNLOCK(seq);
priv->stats.memoryFailures++;
} else
ng_l2tp_xmit_ctrl(priv, m, seq->ns++);
CURVNET_RESTORE();
NET_EPOCH_EXIT(et);
}
static int
ng_l2tp_xmit_ctrl(priv_p priv, struct mbuf *m, u_int16_t ns)
{
struct l2tp_seq *const seq = &priv->seq;
uint8_t *p;
uint16_t nr, session_id = 0;
int error;
SEQ_LOCK_ASSERT(seq);
if (callout_active(&seq->xack_timer))
(void )callout_stop(&seq->xack_timer);
nr = seq->xack = seq->nr;
SEQ_UNLOCK(seq);
if (m == NULL) {
MGETHDR(m, M_NOWAIT, MT_DATA);
if (m == NULL) {
priv->stats.memoryFailures++;
return (ENOBUFS);
}
m->m_len = m->m_pkthdr.len = 12;
m->m_pkthdr.rcvif = NULL;
priv->stats.xmitZLBs++;
} else {
if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
priv->stats.memoryFailures++;
return (ENOBUFS);
}
session_id = (mtod(m, u_int8_t *)[0] << 8) + mtod(m, u_int8_t *)[1];
M_PREPEND(m, 10, M_NOWAIT);
if (m == NULL) {
priv->stats.memoryFailures++;
return (ENOBUFS);
}
m = m_pullup(m, 12);
if (m == NULL) {
priv->stats.memoryFailures++;
return (ENOBUFS);
}
}
p = mtod(m, u_int8_t *);
p[0] = L2TP_CTRL_HDR >> 8;
p[1] = L2TP_CTRL_HDR & 0xff;
p[2] = m->m_pkthdr.len >> 8;
p[3] = m->m_pkthdr.len & 0xff;
p[4] = priv->conf.peer_id >> 8;
p[5] = priv->conf.peer_id & 0xff;
p[6] = session_id >> 8;
p[7] = session_id & 0xff;
p[8] = ns >> 8;
p[9] = ns & 0xff;
p[10] = nr >> 8;
p[11] = nr & 0xff;
priv->stats.xmitPackets++;
priv->stats.xmitOctets += m->m_pkthdr.len;
NG_SEND_DATA_ONLY(error, priv->lower, m);
return (error);
}
#ifdef INVARIANTS
static void
ng_l2tp_seq_check(struct l2tp_seq *seq)
{
int self_unack, peer_unack;
int i;
#define CHECK(p) KASSERT((p), ("%s: not: %s", __func__, #p))
SEQ_LOCK_ASSERT(seq);
self_unack = L2TP_SEQ_DIFF(seq->nr, seq->xack);
peer_unack = L2TP_SEQ_DIFF(seq->ns, seq->rack);
CHECK(seq->wmax <= L2TP_MAX_XWIN);
CHECK(seq->cwnd >= 1);
CHECK(seq->cwnd <= seq->wmax);
CHECK(seq->ssth >= 1);
CHECK(seq->ssth <= seq->wmax);
if (seq->cwnd < seq->ssth)
CHECK(seq->acks == 0);
else
CHECK(seq->acks <= seq->cwnd);
CHECK(self_unack >= 0);
CHECK(peer_unack >= 0);
CHECK(peer_unack <= seq->wmax);
CHECK((self_unack == 0) ^ callout_active(&seq->xack_timer));
CHECK((peer_unack == 0) ^ callout_active(&seq->rack_timer));
for (i = 0; i < peer_unack; i++)
CHECK(seq->xwin[i] != NULL);
for ( ; i < seq->cwnd; i++)
CHECK(seq->xwin[i] == NULL);
#undef CHECK
}
#endif