#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/time.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/errno.h>
#include <sys/ctype.h>
#include <sys/thread2.h>
#include <machine/limits.h>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include <netgraph/ng_parse.h>
#include "ng_ppp.h"
#include <netgraph/vjc/ng_vjc.h>
#define PROT_VALID(p) (((p) & 0x0101) == 0x0001)
#define PROT_COMPRESSABLE(p) (((p) & 0xff00) == 0x0000)
#define PROT_APPLETALK 0x0029
#define PROT_COMPD 0x00fd
#define PROT_CRYPTD 0x0053
#define PROT_IP 0x0021
#define PROT_IPV6 0x0057
#define PROT_IPX 0x002b
#define PROT_LCP 0xc021
#define PROT_MP 0x003d
#define PROT_VJCOMP 0x002d
#define PROT_VJUNCOMP 0x002f
#define MP_MIN_MRRU 1500
#define MP_INITIAL_SEQ 0
#define MP_MIN_LINK_MRU 32
#define MP_SHORT_SEQ_MASK 0x00000fff
#define MP_SHORT_SEQ_HIBIT 0x00000800
#define MP_SHORT_FIRST_FLAG 0x00008000
#define MP_SHORT_LAST_FLAG 0x00004000
#define MP_LONG_SEQ_MASK 0x00ffffff
#define MP_LONG_SEQ_HIBIT 0x00800000
#define MP_LONG_FIRST_FLAG 0x80000000
#define MP_LONG_LAST_FLAG 0x40000000
#define MP_NOSEQ 0x7fffffff
#define MP_SHORT_EXTEND(s) (((s) & MP_SHORT_SEQ_HIBIT) ? \
((s) | ~MP_SHORT_SEQ_MASK) \
: ((s) & MP_SHORT_SEQ_MASK))
#define MP_LONG_EXTEND(s) (((s) & MP_LONG_SEQ_HIBIT) ? \
((s) | ~MP_LONG_SEQ_MASK) \
: ((s) & MP_LONG_SEQ_MASK))
#define MP_SHORT_SEQ_DIFF(x,y) MP_SHORT_EXTEND((x) - (y))
#define MP_LONG_SEQ_DIFF(x,y) MP_LONG_EXTEND((x) - (y))
#define MP_RECV_SEQ_DIFF(priv,x,y) \
((priv)->conf.recvShortSeq ? \
MP_SHORT_SEQ_DIFF((x), (y)) : \
MP_LONG_SEQ_DIFF((x), (y)))
#define MP_NEXT_RECV_SEQ(priv,seq) \
((priv)->conf.recvShortSeq ? \
MP_SHORT_EXTEND((seq) + 1) : \
MP_LONG_EXTEND((seq) + 1))
#define MP_MIN_FRAG_LEN 6
#define MP_MAX_QUEUE_LEN 128
#define MP_FRAGTIMER_INTERVAL (hz/2)
struct ng_ppp_frag {
int seq;
u_char first;
u_char last;
struct timeval timestamp;
struct mbuf *data;
meta_p meta;
TAILQ_ENTRY(ng_ppp_frag) f_qent;
};
static const char *const ng_ppp_hook_names[] = {
NG_PPP_HOOK_ATALK,
#define HOOK_INDEX_ATALK 0
NG_PPP_HOOK_BYPASS,
#define HOOK_INDEX_BYPASS 1
NG_PPP_HOOK_COMPRESS,
#define HOOK_INDEX_COMPRESS 2
NG_PPP_HOOK_ENCRYPT,
#define HOOK_INDEX_ENCRYPT 3
NG_PPP_HOOK_DECOMPRESS,
#define HOOK_INDEX_DECOMPRESS 4
NG_PPP_HOOK_DECRYPT,
#define HOOK_INDEX_DECRYPT 5
NG_PPP_HOOK_INET,
#define HOOK_INDEX_INET 6
NG_PPP_HOOK_IPX,
#define HOOK_INDEX_IPX 7
NG_PPP_HOOK_VJC_COMP,
#define HOOK_INDEX_VJC_COMP 8
NG_PPP_HOOK_VJC_IP,
#define HOOK_INDEX_VJC_IP 9
NG_PPP_HOOK_VJC_UNCOMP,
#define HOOK_INDEX_VJC_UNCOMP 10
NG_PPP_HOOK_VJC_VJIP,
#define HOOK_INDEX_VJC_VJIP 11
NG_PPP_HOOK_IPV6,
#define HOOK_INDEX_IPV6 12
NULL
#define HOOK_INDEX_MAX 13
};
#define HOOK_INDEX(hook) (*((int16_t *) &(hook)->private))
struct ng_ppp_link {
struct ng_ppp_link_conf conf;
hook_p hook;
int32_t seq;
struct timeval lastWrite;
int bytesInQueue;
struct ng_ppp_link_stat stats;
};
struct ng_ppp_private {
struct ng_ppp_bund_conf conf;
struct ng_ppp_link_stat bundleStats;
struct ng_ppp_link links[NG_PPP_MAX_LINKS];
int32_t xseq;
int32_t mseq;
u_char vjCompHooked;
u_char allLinksEqual;
u_char timerActive;
u_int numActiveLinks;
int activeLinks[NG_PPP_MAX_LINKS];
u_int lastLink;
hook_p hooks[HOOK_INDEX_MAX];
TAILQ_HEAD(ng_ppp_fraglist, ng_ppp_frag)
frags;
int qlen;
struct callout fragTimer;
};
typedef struct ng_ppp_private *priv_p;
static ng_constructor_t ng_ppp_constructor;
static ng_rcvmsg_t ng_ppp_rcvmsg;
static ng_shutdown_t ng_ppp_rmnode;
static ng_newhook_t ng_ppp_newhook;
static ng_rcvdata_t ng_ppp_rcvdata;
static ng_disconnect_t ng_ppp_disconnect;
static int ng_ppp_input(node_p node, int bypass,
int linkNum, struct mbuf *m, meta_p meta);
static int ng_ppp_output(node_p node, int bypass, int proto,
int linkNum, struct mbuf *m, meta_p meta);
static int ng_ppp_mp_input(node_p node, int linkNum,
struct mbuf *m, meta_p meta);
static int ng_ppp_check_packet(node_p node);
static void ng_ppp_get_packet(node_p node, struct mbuf **mp, meta_p *metap);
static int ng_ppp_frag_process(node_p node);
static int ng_ppp_frag_trim(node_p node);
static void ng_ppp_frag_timeout(void *arg);
static void ng_ppp_frag_checkstale(node_p node);
static void ng_ppp_frag_reset(node_p node);
static int ng_ppp_mp_output(node_p node, struct mbuf *m, meta_p meta);
static void ng_ppp_mp_strategy(node_p node, int len, int *distrib);
static int ng_ppp_intcmp(const void *v1, const void *v2);
static struct mbuf *ng_ppp_addproto(struct mbuf *m, int proto, int compOK);
static struct mbuf *ng_ppp_prepend(struct mbuf *m, const void *buf, int len);
static int ng_ppp_config_valid(node_p node,
const struct ng_ppp_node_conf *newConf);
static void ng_ppp_update(node_p node, int newConf);
static void ng_ppp_start_frag_timer(node_p node);
static void ng_ppp_stop_frag_timer(node_p node);
static const struct ng_parse_fixedarray_info ng_ppp_rseq_array_info = {
&ng_parse_hint32_type,
NG_PPP_MAX_LINKS
};
static const struct ng_parse_type ng_ppp_rseq_array_type = {
&ng_parse_fixedarray_type,
&ng_ppp_rseq_array_info,
};
static const struct ng_parse_struct_field ng_ppp_mp_state_type_fields[]
= NG_PPP_MP_STATE_TYPE_INFO(&ng_ppp_rseq_array_type);
static const struct ng_parse_type ng_ppp_mp_state_type = {
&ng_parse_struct_type,
&ng_ppp_mp_state_type_fields
};
static const struct ng_parse_struct_field ng_ppp_link_type_fields[]
= NG_PPP_LINK_TYPE_INFO;
static const struct ng_parse_type ng_ppp_link_type = {
&ng_parse_struct_type,
&ng_ppp_link_type_fields
};
static const struct ng_parse_struct_field ng_ppp_bund_type_fields[]
= NG_PPP_BUND_TYPE_INFO;
static const struct ng_parse_type ng_ppp_bund_type = {
&ng_parse_struct_type,
&ng_ppp_bund_type_fields
};
static const struct ng_parse_fixedarray_info ng_ppp_array_info = {
&ng_ppp_link_type,
NG_PPP_MAX_LINKS
};
static const struct ng_parse_type ng_ppp_link_array_type = {
&ng_parse_fixedarray_type,
&ng_ppp_array_info,
};
static const struct ng_parse_struct_field ng_ppp_conf_type_fields[]
= NG_PPP_CONFIG_TYPE_INFO(&ng_ppp_bund_type, &ng_ppp_link_array_type);
static const struct ng_parse_type ng_ppp_conf_type = {
&ng_parse_struct_type,
&ng_ppp_conf_type_fields
};
static const struct ng_parse_struct_field ng_ppp_stats_type_fields[]
= NG_PPP_STATS_TYPE_INFO;
static const struct ng_parse_type ng_ppp_stats_type = {
&ng_parse_struct_type,
&ng_ppp_stats_type_fields
};
static const struct ng_cmdlist ng_ppp_cmds[] = {
{
NGM_PPP_COOKIE,
NGM_PPP_SET_CONFIG,
"setconfig",
&ng_ppp_conf_type,
NULL
},
{
NGM_PPP_COOKIE,
NGM_PPP_GET_CONFIG,
"getconfig",
NULL,
&ng_ppp_conf_type
},
{
NGM_PPP_COOKIE,
NGM_PPP_GET_MP_STATE,
"getmpstate",
NULL,
&ng_ppp_mp_state_type
},
{
NGM_PPP_COOKIE,
NGM_PPP_GET_LINK_STATS,
"getstats",
&ng_parse_int16_type,
&ng_ppp_stats_type
},
{
NGM_PPP_COOKIE,
NGM_PPP_CLR_LINK_STATS,
"clrstats",
&ng_parse_int16_type,
NULL
},
{
NGM_PPP_COOKIE,
NGM_PPP_GETCLR_LINK_STATS,
"getclrstats",
&ng_parse_int16_type,
&ng_ppp_stats_type
},
{ 0 }
};
static struct ng_type ng_ppp_typestruct = {
NG_VERSION,
NG_PPP_NODE_TYPE,
NULL,
ng_ppp_constructor,
ng_ppp_rcvmsg,
ng_ppp_rmnode,
ng_ppp_newhook,
NULL,
NULL,
ng_ppp_rcvdata,
ng_ppp_rcvdata,
ng_ppp_disconnect,
ng_ppp_cmds
};
NETGRAPH_INIT(ppp, &ng_ppp_typestruct);
static int *compareLatencies;
static const u_char ng_ppp_acf[2] = { 0xff, 0x03 };
static const struct timeval ng_ppp_max_staleness = { 2, 0 };
#define ERROUT(x) do { error = (x); goto done; } while (0)
static int
ng_ppp_constructor(node_p *nodep)
{
priv_p priv;
int i, error;
priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
if (priv == NULL)
return (ENOMEM);
if ((error = ng_make_node_common(&ng_ppp_typestruct, nodep))) {
kfree(priv, M_NETGRAPH);
return (error);
}
(*nodep)->private = priv;
TAILQ_INIT(&priv->frags);
for (i = 0; i < NG_PPP_MAX_LINKS; i++)
priv->links[i].seq = MP_NOSEQ;
callout_init(&priv->fragTimer);
return (0);
}
static int
ng_ppp_newhook(node_p node, hook_p hook, const char *name)
{
const priv_p priv = node->private;
int linkNum = -1;
hook_p *hookPtr = NULL;
int hookIndex = -1;
if (strncmp(name, NG_PPP_HOOK_LINK_PREFIX,
strlen(NG_PPP_HOOK_LINK_PREFIX)) == 0) {
const char *cp;
char *eptr;
cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX);
if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
return (EINVAL);
linkNum = (int)strtoul(cp, &eptr, 10);
if (*eptr != '\0' || linkNum < 0 || linkNum >= NG_PPP_MAX_LINKS)
return (EINVAL);
hookPtr = &priv->links[linkNum].hook;
hookIndex = ~linkNum;
} else {
int i;
for (i = 0; ng_ppp_hook_names[i] != NULL; i++) {
if (strcmp(name, ng_ppp_hook_names[i]) == 0) {
hookPtr = &priv->hooks[i];
hookIndex = i;
break;
}
}
if (ng_ppp_hook_names[i] == NULL)
return (EINVAL);
}
if (*hookPtr != NULL)
return (EISCONN);
if (linkNum != -1 && priv->links[linkNum].conf.enableLink
&& !priv->conf.enableMultilink && priv->numActiveLinks >= 1)
return (ENODEV);
*hookPtr = hook;
HOOK_INDEX(hook) = hookIndex;
ng_ppp_update(node, 0);
return (0);
}
static int
ng_ppp_rcvmsg(node_p node, struct ng_mesg *msg,
const char *raddr, struct ng_mesg **rptr)
{
const priv_p priv = node->private;
struct ng_mesg *resp = NULL;
int error = 0;
switch (msg->header.typecookie) {
case NGM_PPP_COOKIE:
switch (msg->header.cmd) {
case NGM_PPP_SET_CONFIG:
{
struct ng_ppp_node_conf *const conf =
(struct ng_ppp_node_conf *)msg->data;
int i;
if (msg->header.arglen != sizeof(*conf))
ERROUT(EINVAL);
if (!ng_ppp_config_valid(node, conf))
ERROUT(EINVAL);
priv->conf = conf->bund;
for (i = 0; i < NG_PPP_MAX_LINKS; i++)
priv->links[i].conf = conf->links[i];
ng_ppp_update(node, 1);
break;
}
case NGM_PPP_GET_CONFIG:
{
struct ng_ppp_node_conf *conf;
int i;
NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
if (resp == NULL)
ERROUT(ENOMEM);
conf = (struct ng_ppp_node_conf *)resp->data;
conf->bund = priv->conf;
for (i = 0; i < NG_PPP_MAX_LINKS; i++)
conf->links[i] = priv->links[i].conf;
break;
}
case NGM_PPP_GET_MP_STATE:
{
struct ng_ppp_mp_state *info;
int i;
NG_MKRESPONSE(resp, msg, sizeof(*info), M_NOWAIT);
if (resp == NULL)
ERROUT(ENOMEM);
info = (struct ng_ppp_mp_state *)resp->data;
bzero(info, sizeof(*info));
for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
if (priv->links[i].seq != MP_NOSEQ)
info->rseq[i] = priv->links[i].seq;
}
info->mseq = priv->mseq;
info->xseq = priv->xseq;
break;
}
case NGM_PPP_GET_LINK_STATS:
case NGM_PPP_CLR_LINK_STATS:
case NGM_PPP_GETCLR_LINK_STATS:
{
struct ng_ppp_link_stat *stats;
u_int16_t linkNum;
if (msg->header.arglen != sizeof(u_int16_t))
ERROUT(EINVAL);
linkNum = *((u_int16_t *) msg->data);
if (linkNum >= NG_PPP_MAX_LINKS
&& linkNum != NG_PPP_BUNDLE_LINKNUM)
ERROUT(EINVAL);
stats = (linkNum == NG_PPP_BUNDLE_LINKNUM) ?
&priv->bundleStats : &priv->links[linkNum].stats;
if (msg->header.cmd != NGM_PPP_CLR_LINK_STATS) {
NG_MKRESPONSE(resp, msg,
sizeof(struct ng_ppp_link_stat), M_NOWAIT);
if (resp == NULL)
ERROUT(ENOMEM);
bcopy(stats, resp->data, sizeof(*stats));
}
if (msg->header.cmd != NGM_PPP_GET_LINK_STATS)
bzero(stats, sizeof(*stats));
break;
}
default:
error = EINVAL;
break;
}
break;
case NGM_VJC_COOKIE:
{
char path[NG_PATHSIZ];
node_p origNode;
if ((error = ng_path2node(node, raddr, &origNode, NULL)) != 0)
ERROUT(error);
ksnprintf(path, sizeof(path), "[%lx]:%s",
(long)node, NG_PPP_HOOK_VJC_IP);
return ng_send_msg(origNode, msg, path, rptr);
}
default:
error = EINVAL;
break;
}
if (rptr)
*rptr = resp;
else if (resp)
kfree(resp, M_NETGRAPH);
done:
kfree(msg, M_NETGRAPH);
return (error);
}
static int
ng_ppp_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
{
const node_p node = hook->node;
const priv_p priv = node->private;
const int index = HOOK_INDEX(hook);
u_int16_t linkNum = NG_PPP_BUNDLE_LINKNUM;
hook_p outHook = NULL;
int proto = 0, error;
if (index < 0) {
struct ng_ppp_link *link;
linkNum = (u_int16_t)~index;
KASSERT(linkNum < NG_PPP_MAX_LINKS,
("%s: bogus index 0x%x", __func__, index));
link = &priv->links[linkNum];
link->stats.recvFrames++;
link->stats.recvOctets += m->m_pkthdr.len;
if (m->m_pkthdr.len >= 2) {
if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
NG_FREE_DATA(m, meta);
return (ENOBUFS);
}
if (bcmp(mtod(m, u_char *), &ng_ppp_acf, 2) == 0)
m_adj(m, 2);
}
return ng_ppp_input(node,
!link->conf.enableLink, linkNum, m, meta);
}
switch (index) {
case HOOK_INDEX_ATALK:
if (!priv->conf.enableAtalk) {
NG_FREE_DATA(m, meta);
return (ENXIO);
}
proto = PROT_APPLETALK;
break;
case HOOK_INDEX_IPX:
if (!priv->conf.enableIPX) {
NG_FREE_DATA(m, meta);
return (ENXIO);
}
proto = PROT_IPX;
break;
case HOOK_INDEX_IPV6:
if (!priv->conf.enableIPv6) {
NG_FREE_DATA(m, meta);
return (ENXIO);
}
proto = PROT_IPV6;
break;
case HOOK_INDEX_INET:
case HOOK_INDEX_VJC_VJIP:
if (!priv->conf.enableIP) {
NG_FREE_DATA(m, meta);
return (ENXIO);
}
proto = PROT_IP;
break;
case HOOK_INDEX_VJC_COMP:
if (!priv->conf.enableVJCompression) {
NG_FREE_DATA(m, meta);
return (ENXIO);
}
proto = PROT_VJCOMP;
break;
case HOOK_INDEX_VJC_UNCOMP:
if (!priv->conf.enableVJCompression) {
NG_FREE_DATA(m, meta);
return (ENXIO);
}
proto = PROT_VJUNCOMP;
break;
case HOOK_INDEX_COMPRESS:
if (!priv->conf.enableCompression) {
NG_FREE_DATA(m, meta);
return (ENXIO);
}
proto = PROT_COMPD;
break;
case HOOK_INDEX_ENCRYPT:
if (!priv->conf.enableEncryption) {
NG_FREE_DATA(m, meta);
return (ENXIO);
}
proto = PROT_CRYPTD;
break;
case HOOK_INDEX_BYPASS:
if (m->m_pkthdr.len < 4) {
NG_FREE_DATA(m, meta);
return (EINVAL);
}
if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
NG_FREE_META(meta);
return (ENOBUFS);
}
linkNum = ntohs(mtod(m, u_int16_t *)[0]);
proto = ntohs(mtod(m, u_int16_t *)[1]);
m_adj(m, 4);
if (linkNum >= NG_PPP_MAX_LINKS
&& linkNum != NG_PPP_BUNDLE_LINKNUM) {
NG_FREE_DATA(m, meta);
return (EINVAL);
}
break;
case HOOK_INDEX_VJC_IP:
if (!priv->conf.enableIP || !priv->conf.enableVJDecompression) {
NG_FREE_DATA(m, meta);
return (ENXIO);
}
break;
case HOOK_INDEX_DECOMPRESS:
if (!priv->conf.enableDecompression) {
NG_FREE_DATA(m, meta);
return (ENXIO);
}
break;
case HOOK_INDEX_DECRYPT:
if (!priv->conf.enableDecryption) {
NG_FREE_DATA(m, meta);
return (ENXIO);
}
break;
default:
panic("%s: bogus index 0x%x", __func__, index);
}
switch (index) {
case HOOK_INDEX_INET:
if (priv->conf.enableVJCompression && priv->vjCompHooked) {
outHook = priv->hooks[HOOK_INDEX_VJC_IP];
break;
}
case HOOK_INDEX_ATALK:
case HOOK_INDEX_IPV6:
case HOOK_INDEX_IPX:
case HOOK_INDEX_VJC_COMP:
case HOOK_INDEX_VJC_UNCOMP:
case HOOK_INDEX_VJC_VJIP:
if (priv->conf.enableCompression
&& priv->hooks[HOOK_INDEX_COMPRESS] != NULL) {
if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) {
NG_FREE_META(meta);
return (ENOBUFS);
}
outHook = priv->hooks[HOOK_INDEX_COMPRESS];
break;
}
case HOOK_INDEX_COMPRESS:
if (priv->conf.enableEncryption
&& priv->hooks[HOOK_INDEX_ENCRYPT] != NULL) {
if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) {
NG_FREE_META(meta);
return (ENOBUFS);
}
outHook = priv->hooks[HOOK_INDEX_ENCRYPT];
break;
}
case HOOK_INDEX_ENCRYPT:
return ng_ppp_output(node, 0,
proto, NG_PPP_BUNDLE_LINKNUM, m, meta);
case HOOK_INDEX_BYPASS:
return ng_ppp_output(node, 1, proto, linkNum, m, meta);
case HOOK_INDEX_DECRYPT:
case HOOK_INDEX_DECOMPRESS:
return ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, m, meta);
case HOOK_INDEX_VJC_IP:
outHook = priv->hooks[HOOK_INDEX_INET];
break;
}
NG_SEND_DATA(error, outHook, m, meta);
return (error);
}
static int
ng_ppp_rmnode(node_p node)
{
const priv_p priv = node->private;
ng_ppp_stop_frag_timer(node);
node->flags |= NG_INVALID;
ng_cutlinks(node);
ng_unname(node);
ng_ppp_frag_reset(node);
bzero(priv, sizeof(*priv));
kfree(priv, M_NETGRAPH);
node->private = NULL;
ng_unref(node);
return (0);
}
static int
ng_ppp_disconnect(hook_p hook)
{
const node_p node = hook->node;
const priv_p priv = node->private;
const int index = HOOK_INDEX(hook);
if (index < 0)
priv->links[~index].hook = NULL;
else
priv->hooks[index] = NULL;
if (node->numhooks > 0)
ng_ppp_update(node, 0);
else
ng_rmnode(node);
return (0);
}
static int
ng_ppp_input(node_p node, int bypass, int linkNum, struct mbuf *m, meta_p meta)
{
const priv_p priv = node->private;
hook_p outHook = NULL;
int proto, error;
for (proto = 0; !PROT_VALID(proto) && m->m_pkthdr.len > 0; ) {
if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL) {
NG_FREE_META(meta);
return (ENOBUFS);
}
proto = (proto << 8) + *mtod(m, u_char *);
m_adj(m, 1);
}
if (!PROT_VALID(proto)) {
if (linkNum == NG_PPP_BUNDLE_LINKNUM)
priv->bundleStats.badProtos++;
else
priv->links[linkNum].stats.badProtos++;
NG_FREE_DATA(m, meta);
return (EINVAL);
}
if (bypass)
goto bypass;
switch (proto) {
case PROT_COMPD:
if (priv->conf.enableDecompression)
outHook = priv->hooks[HOOK_INDEX_DECOMPRESS];
break;
case PROT_CRYPTD:
if (priv->conf.enableDecryption)
outHook = priv->hooks[HOOK_INDEX_DECRYPT];
break;
case PROT_VJCOMP:
if (priv->conf.enableVJDecompression && priv->vjCompHooked)
outHook = priv->hooks[HOOK_INDEX_VJC_COMP];
break;
case PROT_VJUNCOMP:
if (priv->conf.enableVJDecompression && priv->vjCompHooked)
outHook = priv->hooks[HOOK_INDEX_VJC_UNCOMP];
break;
case PROT_MP:
if (priv->conf.enableMultilink
&& linkNum != NG_PPP_BUNDLE_LINKNUM)
return ng_ppp_mp_input(node, linkNum, m, meta);
break;
case PROT_APPLETALK:
if (priv->conf.enableAtalk)
outHook = priv->hooks[HOOK_INDEX_ATALK];
break;
case PROT_IPX:
if (priv->conf.enableIPX)
outHook = priv->hooks[HOOK_INDEX_IPX];
break;
case PROT_IP:
if (priv->conf.enableIP)
outHook = priv->hooks[HOOK_INDEX_INET];
break;
case PROT_IPV6:
if (priv->conf.enableIPv6)
outHook = priv->hooks[HOOK_INDEX_IPV6];
break;
}
bypass:
if (outHook == NULL) {
u_int16_t hdr[2];
hdr[0] = htons(linkNum);
hdr[1] = htons((u_int16_t)proto);
if ((m = ng_ppp_prepend(m, &hdr, 4)) == NULL) {
NG_FREE_META(meta);
return (ENOBUFS);
}
outHook = priv->hooks[HOOK_INDEX_BYPASS];
}
NG_SEND_DATA(error, outHook, m, meta);
return (error);
}
static int
ng_ppp_output(node_p node, int bypass,
int proto, int linkNum, struct mbuf *m, meta_p meta)
{
const priv_p priv = node->private;
struct ng_ppp_link *link;
int len, error;
u_int16_t mru;
if (linkNum == NG_PPP_BUNDLE_LINKNUM && !priv->conf.enableMultilink)
linkNum = priv->activeLinks[0];
link = (linkNum != NG_PPP_BUNDLE_LINKNUM) ?
&priv->links[linkNum] : NULL;
if (linkNum != NG_PPP_BUNDLE_LINKNUM) {
if (!bypass && !link->conf.enableLink) {
NG_FREE_DATA(m, meta);
return (ENXIO);
}
if (link->hook == NULL) {
NG_FREE_DATA(m, meta);
return (ENETDOWN);
}
}
mru = (link != NULL) ? link->conf.mru : priv->conf.mrru;
if (mru != 0 && m->m_pkthdr.len > mru) {
NG_FREE_DATA(m, meta);
return (EMSGSIZE);
}
if ((m = ng_ppp_addproto(m, proto,
linkNum == NG_PPP_BUNDLE_LINKNUM
|| link->conf.enableProtoComp)) == NULL) {
NG_FREE_META(meta);
return (ENOBUFS);
}
if (linkNum == NG_PPP_BUNDLE_LINKNUM)
return ng_ppp_mp_output(node, m, meta);
if (proto == PROT_LCP || !link->conf.enableACFComp) {
if ((m = ng_ppp_prepend(m, &ng_ppp_acf, 2)) == NULL) {
NG_FREE_META(meta);
return (ENOBUFS);
}
}
len = m->m_pkthdr.len;
NG_SEND_DATA(error, link->hook, m, meta);
if (error == 0) {
link->stats.xmitFrames++;
link->stats.xmitOctets += len;
link->bytesInQueue += len;
getmicrouptime(&link->lastWrite);
}
return error;
}
static int
ng_ppp_mp_input(node_p node, int linkNum, struct mbuf *m, meta_p meta)
{
const priv_p priv = node->private;
struct ng_ppp_link *const link = &priv->links[linkNum];
struct ng_ppp_frag frag0, *frag = &frag0;
struct ng_ppp_frag *qent;
int i, diff, inserted;
priv->bundleStats.recvFrames++;
priv->bundleStats.recvOctets += m->m_pkthdr.len;
if (priv->conf.recvShortSeq) {
u_int16_t shdr;
if (m->m_pkthdr.len < 2) {
link->stats.runts++;
NG_FREE_DATA(m, meta);
return (EINVAL);
}
if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
NG_FREE_META(meta);
return (ENOBUFS);
}
shdr = ntohs(*mtod(m, u_int16_t *));
frag->seq = MP_SHORT_EXTEND(shdr);
frag->first = (shdr & MP_SHORT_FIRST_FLAG) != 0;
frag->last = (shdr & MP_SHORT_LAST_FLAG) != 0;
diff = MP_SHORT_SEQ_DIFF(frag->seq, priv->mseq);
m_adj(m, 2);
} else {
u_int32_t lhdr;
if (m->m_pkthdr.len < 4) {
link->stats.runts++;
NG_FREE_DATA(m, meta);
return (EINVAL);
}
if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
NG_FREE_META(meta);
return (ENOBUFS);
}
lhdr = ntohl(*mtod(m, u_int32_t *));
frag->seq = MP_LONG_EXTEND(lhdr);
frag->first = (lhdr & MP_LONG_FIRST_FLAG) != 0;
frag->last = (lhdr & MP_LONG_LAST_FLAG) != 0;
diff = MP_LONG_SEQ_DIFF(frag->seq, priv->mseq);
m_adj(m, 4);
}
frag->data = m;
frag->meta = meta;
getmicrouptime(&frag->timestamp);
if (diff < 0) {
link->stats.dropFragments++;
NG_FREE_DATA(m, meta);
return (0);
}
priv->mseq = link->seq = frag->seq;
for (i = 0; i < priv->numActiveLinks; i++) {
struct ng_ppp_link *const alink =
&priv->links[priv->activeLinks[i]];
if (MP_RECV_SEQ_DIFF(priv, alink->seq, priv->mseq) < 0)
priv->mseq = alink->seq;
}
frag = kmalloc(sizeof(*frag), M_NETGRAPH, M_NOWAIT);
if (frag == NULL) {
NG_FREE_DATA(m, meta);
ng_ppp_frag_process(node);
return (ENOMEM);
}
*frag = frag0;
inserted = 0;
TAILQ_FOREACH_REVERSE(qent, &priv->frags, ng_ppp_fraglist, f_qent) {
diff = MP_RECV_SEQ_DIFF(priv, frag->seq, qent->seq);
if (diff > 0) {
TAILQ_INSERT_AFTER(&priv->frags, qent, frag, f_qent);
inserted = 1;
break;
} else if (diff == 0) {
link->stats.dupFragments++;
NG_FREE_DATA(frag->data, frag->meta);
kfree(frag, M_NETGRAPH);
return (EINVAL);
}
}
if (!inserted)
TAILQ_INSERT_HEAD(&priv->frags, frag, f_qent);
priv->qlen++;
return ng_ppp_frag_process(node);
}
static int
ng_ppp_check_packet(node_p node)
{
const priv_p priv = node->private;
struct ng_ppp_frag *qent, *qnext;
if (TAILQ_EMPTY(&priv->frags))
return (0);
qent = TAILQ_FIRST(&priv->frags);
if (!qent->first || MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) > 1)
return (0);
while (!qent->last) {
qnext = TAILQ_NEXT(qent, f_qent);
if (qnext == NULL)
return (0);
if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq))
return (0);
qent = qnext;
}
return (1);
}
static void
ng_ppp_get_packet(node_p node, struct mbuf **mp, meta_p *metap)
{
const priv_p priv = node->private;
struct ng_ppp_frag *qent, *qnext;
struct mbuf *m = NULL, *tail;
qent = TAILQ_FIRST(&priv->frags);
KASSERT(!TAILQ_EMPTY(&priv->frags) && qent->first,
("%s: no packet", __func__));
for (tail = NULL; qent != NULL; qent = qnext) {
qnext = TAILQ_NEXT(qent, f_qent);
KASSERT(!TAILQ_EMPTY(&priv->frags),
("%s: empty q", __func__));
TAILQ_REMOVE(&priv->frags, qent, f_qent);
if (tail == NULL) {
tail = m = qent->data;
*metap = qent->meta;
} else {
m->m_pkthdr.len += qent->data->m_pkthdr.len;
tail->m_next = qent->data;
NG_FREE_META(qent->meta);
}
while (tail->m_next != NULL)
tail = tail->m_next;
if (qent->last)
qnext = NULL;
kfree(qent, M_NETGRAPH);
priv->qlen--;
}
*mp = m;
}
static int
ng_ppp_frag_trim(node_p node)
{
const priv_p priv = node->private;
struct ng_ppp_frag *qent, *qnext = NULL;
int removed = 0;
while (1) {
int dead = 0;
if (TAILQ_EMPTY(&priv->frags))
break;
TAILQ_FOREACH(qent, &priv->frags, f_qent) {
if (MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) >= 0)
break;
qnext = TAILQ_NEXT(qent, f_qent);
KASSERT(qnext != NULL,
("%s: last frag < MSEQ?", __func__));
if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq)
|| qent->last || qnext->first) {
dead = 1;
break;
}
}
if (!dead)
break;
while ((qent = TAILQ_FIRST(&priv->frags)) != qnext) {
KASSERT(!TAILQ_EMPTY(&priv->frags),
("%s: empty q", __func__));
priv->bundleStats.dropFragments++;
TAILQ_REMOVE(&priv->frags, qent, f_qent);
NG_FREE_DATA(qent->data, qent->meta);
kfree(qent, M_NETGRAPH);
priv->qlen--;
removed = 1;
}
}
return (removed);
}
static int
ng_ppp_frag_process(node_p node)
{
const priv_p priv = node->private;
struct mbuf *m;
meta_p meta;
while (ng_ppp_check_packet(node)) {
ng_ppp_get_packet(node, &m, &meta);
ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, m, meta);
}
if (ng_ppp_frag_trim(node)) {
while (ng_ppp_check_packet(node)) {
ng_ppp_get_packet(node, &m, &meta);
ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, m, meta);
}
}
ng_ppp_frag_checkstale(node);
if (priv->qlen > MP_MAX_QUEUE_LEN) {
struct ng_ppp_frag *qent;
int i;
KASSERT(!TAILQ_EMPTY(&priv->frags),
("%s: empty q", __func__));
qent = TAILQ_FIRST(&priv->frags);
if (MP_RECV_SEQ_DIFF(priv, priv->mseq, qent->seq) < 0) {
priv->mseq = qent->seq;
for (i = 0; i < priv->numActiveLinks; i++) {
struct ng_ppp_link *const alink =
&priv->links[priv->activeLinks[i]];
if (MP_RECV_SEQ_DIFF(priv,
alink->seq, priv->mseq) < 0)
alink->seq = priv->mseq;
}
}
priv->bundleStats.dropFragments++;
TAILQ_REMOVE(&priv->frags, qent, f_qent);
NG_FREE_DATA(qent->data, qent->meta);
kfree(qent, M_NETGRAPH);
priv->qlen--;
return ng_ppp_frag_process(node);
}
return (0);
}
static void
ng_ppp_frag_checkstale(node_p node)
{
const priv_p priv = node->private;
struct ng_ppp_frag *qent, *beg, *end;
struct timeval now, age;
struct mbuf *m;
meta_p meta;
int i, seq;
int endseq;
now.tv_sec = 0;
while (1) {
if (TAILQ_EMPTY(&priv->frags))
break;
beg = end = NULL;
seq = TAILQ_FIRST(&priv->frags)->seq;
TAILQ_FOREACH(qent, &priv->frags, f_qent) {
if (qent->first)
beg = qent;
else if (qent->seq != seq)
beg = NULL;
if (beg != NULL && qent->last) {
end = qent;
break;
}
seq = MP_NEXT_RECV_SEQ(priv, seq);
}
if (end == NULL)
break;
if (now.tv_sec == 0)
getmicrouptime(&now);
age = now;
timevalsub(&age, &beg->timestamp);
if (timevalcmp(&age, &ng_ppp_max_staleness, < ))
break;
while ((qent = TAILQ_FIRST(&priv->frags)) != beg) {
KASSERT(!TAILQ_EMPTY(&priv->frags),
("%s: empty q", __func__));
priv->bundleStats.dropFragments++;
TAILQ_REMOVE(&priv->frags, qent, f_qent);
NG_FREE_DATA(qent->data, qent->meta);
kfree(qent, M_NETGRAPH);
priv->qlen--;
}
endseq = end->seq;
ng_ppp_get_packet(node, &m, &meta);
if (MP_RECV_SEQ_DIFF(priv, priv->mseq, endseq) < 0) {
priv->mseq = endseq;
for (i = 0; i < priv->numActiveLinks; i++) {
struct ng_ppp_link *const alink =
&priv->links[priv->activeLinks[i]];
if (MP_RECV_SEQ_DIFF(priv,
alink->seq, priv->mseq) < 0)
alink->seq = priv->mseq;
}
}
ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, m, meta);
}
}
static void
ng_ppp_frag_timeout(void *arg)
{
const node_p node = arg;
const priv_p priv = node->private;
crit_enter();
if ((node->flags & NG_INVALID) != 0) {
ng_unref(node);
crit_exit();
return;
}
KASSERT(priv->timerActive, ("%s: !timerActive", __func__));
priv->timerActive = 0;
KASSERT(node->refs > 1, ("%s: refs=%d", __func__, node->refs));
ng_unref(node);
ng_ppp_start_frag_timer(node);
ng_ppp_frag_checkstale(node);
crit_exit();
}
static int
ng_ppp_mp_output(node_p node, struct mbuf *m, meta_p meta)
{
const priv_p priv = node->private;
const int hdr_len = priv->conf.xmitShortSeq ? 2 : 4;
int distrib[NG_PPP_MAX_LINKS];
int firstFragment;
int activeLinkNum;
if (priv->numActiveLinks == 0) {
NG_FREE_DATA(m, meta);
return (ENETDOWN);
}
if (priv->conf.enableRoundRobin || m->m_pkthdr.len < MP_MIN_FRAG_LEN) {
activeLinkNum = priv->lastLink++ % priv->numActiveLinks;
bzero(&distrib, priv->numActiveLinks * sizeof(distrib[0]));
distrib[activeLinkNum] = m->m_pkthdr.len;
goto deliver;
}
if (priv->allLinksEqual) {
const int fraction = m->m_pkthdr.len / priv->numActiveLinks;
int i, remain;
for (i = 0; i < priv->numActiveLinks; i++)
distrib[priv->lastLink++ % priv->numActiveLinks]
= fraction;
remain = m->m_pkthdr.len - (fraction * priv->numActiveLinks);
while (remain > 0) {
distrib[priv->lastLink++ % priv->numActiveLinks]++;
remain--;
}
goto deliver;
}
ng_ppp_mp_strategy(node, m->m_pkthdr.len, distrib);
deliver:
priv->bundleStats.xmitFrames++;
priv->bundleStats.xmitOctets += m->m_pkthdr.len;
for (firstFragment = 1, activeLinkNum = priv->numActiveLinks - 1;
activeLinkNum >= 0; activeLinkNum--) {
const int linkNum = priv->activeLinks[activeLinkNum];
struct ng_ppp_link *const link = &priv->links[linkNum];
for ( ; distrib[activeLinkNum] > 0; firstFragment = 0) {
int len, lastFragment, error;
struct mbuf *m2;
meta_p meta2;
len = distrib[activeLinkNum];
if (len > link->conf.mru - hdr_len)
len = link->conf.mru - hdr_len;
distrib[activeLinkNum] -= len;
lastFragment = (len == m->m_pkthdr.len);
m2 = m;
if (!lastFragment) {
struct mbuf *n = m_split(m, len, M_NOWAIT);
if (n == NULL) {
NG_FREE_DATA(m, meta);
return (ENOMEM);
}
m = n;
}
if (priv->conf.xmitShortSeq) {
u_int16_t shdr;
shdr = priv->xseq;
priv->xseq =
(priv->xseq + 1) & MP_SHORT_SEQ_MASK;
if (firstFragment)
shdr |= MP_SHORT_FIRST_FLAG;
if (lastFragment)
shdr |= MP_SHORT_LAST_FLAG;
shdr = htons(shdr);
m2 = ng_ppp_prepend(m2, &shdr, 2);
} else {
u_int32_t lhdr;
lhdr = priv->xseq;
priv->xseq =
(priv->xseq + 1) & MP_LONG_SEQ_MASK;
if (firstFragment)
lhdr |= MP_LONG_FIRST_FLAG;
if (lastFragment)
lhdr |= MP_LONG_LAST_FLAG;
lhdr = htonl(lhdr);
m2 = ng_ppp_prepend(m2, &lhdr, 4);
}
if (m2 == NULL) {
if (!lastFragment)
m_freem(m);
NG_FREE_META(meta);
return (ENOBUFS);
}
meta2 = lastFragment ? meta : ng_copy_meta(meta);
error = ng_ppp_output(node, 0,
PROT_MP, linkNum, m2, meta2);
if (error != 0) {
if (!lastFragment)
NG_FREE_DATA(m, meta);
return (error);
}
}
}
return (0);
}
static void
ng_ppp_mp_strategy(node_p node, int len, int *distrib)
{
const priv_p priv = node->private;
int latency[NG_PPP_MAX_LINKS];
int sortByLatency[NG_PPP_MAX_LINKS];
int activeLinkNum;
int t0, total, topSum, botSum;
struct timeval now;
int i, numFragments;
if (priv->numActiveLinks == 1) {
distrib[0] = len;
return;
}
getmicrouptime(&now);
for (activeLinkNum = 0;
activeLinkNum < priv->numActiveLinks; activeLinkNum++) {
struct ng_ppp_link *alink;
struct timeval diff;
int xmitBytes;
alink = &priv->links[priv->activeLinks[activeLinkNum]];
latency[activeLinkNum] = alink->conf.latency;
sortByLatency[activeLinkNum] = activeLinkNum;
if (alink->bytesInQueue == 0)
continue;
diff = now;
timevalsub(&diff, &alink->lastWrite);
if (now.tv_sec < 0 || diff.tv_sec >= 10) {
alink->bytesInQueue = 0;
continue;
}
xmitBytes = (alink->conf.bandwidth * diff.tv_sec)
+ (alink->conf.bandwidth * (diff.tv_usec / 1000)) / 100;
alink->bytesInQueue -= xmitBytes;
if (alink->bytesInQueue < 0)
alink->bytesInQueue = 0;
else
latency[activeLinkNum] +=
(100 * alink->bytesInQueue) / alink->conf.bandwidth;
}
compareLatencies = latency;
kqsort(sortByLatency,
priv->numActiveLinks, sizeof(*sortByLatency), ng_ppp_intcmp);
compareLatencies = NULL;
for (numFragments = 1;
numFragments < priv->numActiveLinks; numFragments++) {
for (total = i = 0; i < numFragments; i++) {
int flowTime;
flowTime = latency[sortByLatency[numFragments]]
- latency[sortByLatency[i]];
total += ((flowTime * priv->links[
priv->activeLinks[sortByLatency[i]]].conf.bandwidth)
+ 99) / 100;
}
if (total >= len)
break;
}
for (topSum = botSum = i = 0; i < numFragments; i++) {
int bw = priv->links[
priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
topSum += latency[sortByLatency[i]] * bw;
botSum += bw;
}
t0 = ((len * 100) + topSum + botSum / 2) / botSum;
bzero(distrib, priv->numActiveLinks * sizeof(*distrib));
for (total = i = 0; i < numFragments; i++) {
int bw = priv->links[
priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
distrib[sortByLatency[i]] =
(bw * (t0 - latency[sortByLatency[i]]) + 50) / 100;
total += distrib[sortByLatency[i]];
}
if (total < len) {
struct ng_ppp_link *fastLink =
&priv->links[priv->activeLinks[sortByLatency[0]]];
int fast = 0;
for (i = 1; i < numFragments; i++) {
struct ng_ppp_link *const link =
&priv->links[priv->activeLinks[sortByLatency[i]]];
if (link->conf.bandwidth > fastLink->conf.bandwidth) {
fast = i;
fastLink = link;
}
}
distrib[sortByLatency[fast]] += len - total;
} else while (total > len) {
struct ng_ppp_link *slowLink =
&priv->links[priv->activeLinks[sortByLatency[0]]];
int delta, slow = 0;
for (i = 1; i < numFragments; i++) {
struct ng_ppp_link *const link =
&priv->links[priv->activeLinks[sortByLatency[i]]];
if (distrib[sortByLatency[slow]] == 0
|| (distrib[sortByLatency[i]] > 0
&& link->conf.bandwidth <
slowLink->conf.bandwidth)) {
slow = i;
slowLink = link;
}
}
delta = total - len;
if (delta > distrib[sortByLatency[slow]])
delta = distrib[sortByLatency[slow]];
distrib[sortByLatency[slow]] -= delta;
total -= delta;
}
}
static int
ng_ppp_intcmp(const void *v1, const void *v2)
{
const int index1 = *((const int *) v1);
const int index2 = *((const int *) v2);
return compareLatencies[index1] - compareLatencies[index2];
}
static struct mbuf *
ng_ppp_addproto(struct mbuf *m, int proto, int compOK)
{
if (compOK && PROT_COMPRESSABLE(proto)) {
u_char pbyte = (u_char)proto;
return ng_ppp_prepend(m, &pbyte, 1);
} else {
u_int16_t pword = htons((u_int16_t)proto);
return ng_ppp_prepend(m, &pword, 2);
}
}
static struct mbuf *
ng_ppp_prepend(struct mbuf *m, const void *buf, int len)
{
M_PREPEND(m, len, M_NOWAIT);
if (m == NULL || (m->m_len < len && (m = m_pullup(m, len)) == NULL))
return (NULL);
bcopy(buf, mtod(m, u_char *), len);
return (m);
}
static void
ng_ppp_update(node_p node, int newConf)
{
const priv_p priv = node->private;
int i;
priv->vjCompHooked = priv->hooks[HOOK_INDEX_VJC_IP] != NULL
&& priv->hooks[HOOK_INDEX_VJC_COMP] != NULL
&& priv->hooks[HOOK_INDEX_VJC_UNCOMP] != NULL
&& priv->hooks[HOOK_INDEX_VJC_VJIP] != NULL;
if (newConf) {
for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
int hdrBytes;
hdrBytes = (priv->links[i].conf.enableACFComp ? 0 : 2)
+ (priv->links[i].conf.enableProtoComp ? 1 : 2)
+ (priv->conf.xmitShortSeq ? 2 : 4);
priv->links[i].conf.latency +=
((hdrBytes * priv->links[i].conf.bandwidth) + 50)
/ 100;
}
}
bzero(&priv->activeLinks, sizeof(priv->activeLinks));
priv->numActiveLinks = 0;
priv->allLinksEqual = 1;
for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
struct ng_ppp_link *const link = &priv->links[i];
if (link->conf.enableLink && link->hook != NULL) {
struct ng_ppp_link *link0;
priv->activeLinks[priv->numActiveLinks++] = i;
link0 = &priv->links[priv->activeLinks[0]];
if (link->conf.latency != link0->conf.latency
|| link->conf.bandwidth != link0->conf.bandwidth)
priv->allLinksEqual = 0;
if (link->seq == MP_NOSEQ) {
link->seq = (link == link0) ?
MP_INITIAL_SEQ : link0->seq;
}
} else
link->seq = MP_NOSEQ;
}
if (priv->conf.enableMultilink && priv->numActiveLinks > 0)
ng_ppp_start_frag_timer(node);
else {
ng_ppp_stop_frag_timer(node);
ng_ppp_frag_reset(node);
priv->xseq = MP_INITIAL_SEQ;
priv->mseq = MP_INITIAL_SEQ;
for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
struct ng_ppp_link *const link = &priv->links[i];
bzero(&link->lastWrite, sizeof(link->lastWrite));
link->bytesInQueue = 0;
link->seq = MP_NOSEQ;
}
}
}
static int
ng_ppp_config_valid(node_p node, const struct ng_ppp_node_conf *newConf)
{
const priv_p priv = node->private;
int i, newNumLinksActive;
for (newNumLinksActive = i = 0; i < NG_PPP_MAX_LINKS; i++) {
if (newConf->links[i].enableLink && priv->links[i].hook != NULL)
newNumLinksActive++;
if (!newConf->links[i].enableLink)
continue;
if (newConf->links[i].mru < MP_MIN_LINK_MRU)
return (0);
if (newConf->links[i].bandwidth == 0)
return (0);
if (newConf->links[i].bandwidth > NG_PPP_MAX_BANDWIDTH)
return (0);
if (newConf->links[i].latency > NG_PPP_MAX_LATENCY)
return (0);
}
if (newConf->bund.enableMultilink && newConf->bund.mrru < MP_MIN_MRRU)
return (0);
if (priv->numActiveLinks > 0 && newNumLinksActive > 0) {
if (!priv->conf.enableMultilink
!= !newConf->bund.enableMultilink
|| !priv->conf.xmitShortSeq != !newConf->bund.xmitShortSeq
|| !priv->conf.recvShortSeq != !newConf->bund.recvShortSeq)
return (0);
}
if (!newConf->bund.enableMultilink && newNumLinksActive > 1)
return (0);
return (1);
}
static void
ng_ppp_frag_reset(node_p node)
{
const priv_p priv = node->private;
struct ng_ppp_frag *qent, *qnext;
for (qent = TAILQ_FIRST(&priv->frags); qent; qent = qnext) {
qnext = TAILQ_NEXT(qent, f_qent);
NG_FREE_DATA(qent->data, qent->meta);
kfree(qent, M_NETGRAPH);
}
TAILQ_INIT(&priv->frags);
priv->qlen = 0;
}
static void
ng_ppp_start_frag_timer(node_p node)
{
const priv_p priv = node->private;
if (!priv->timerActive) {
callout_reset(&priv->fragTimer, MP_FRAGTIMER_INTERVAL,
ng_ppp_frag_timeout, node);
priv->timerActive = 1;
node->refs++;
}
}
static void
ng_ppp_stop_frag_timer(node_p node)
{
const priv_p priv = node->private;
if (priv->timerActive) {
callout_stop(&priv->fragTimer);
priv->timerActive = 0;
KASSERT(node->refs > 1,
("%s: refs=%d", __func__, node->refs));
ng_unref(node);
}
}