ng_vlan improvements
rozhuk.im at gmail.com
rozhuk.im at gmail.com
Thu Jun 23 22:03:47 UTC 2011
Hi!
Changes:
1. list + hash was replaced by array of hooks: its simple and faster
2. added encap setting.
"getencap" and "setencap" messages for control it:
Default = 1 (do the VLAN encapsulation)
0: no encapsulation, just:
m->m_flags |= M_VLANTAG;
m->m_pkthdr.ether_vtag = (vlan & EVL_VLID_MASK);
Who can test?
--
Rozhuk Ivan
-------------- next part --------------
/*-
* Copyright (c) 2003 IPNET Internet Communication Company
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Author: Ruslan Ermilov <ru at FreeBSD.org>
*
* $FreeBSD: src/sys/netgraph/ng_vlan.c,v 1.6.2.1 2009/08/03 08:13:06 kensmith Exp $
*/
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/systm.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_vlan_var.h>
#include <netgraph/ng_message.h>
#include <netgraph/ng_parse.h>
#include <netgraph/ng_vlan.h>
#include <netgraph/netgraph.h>
static ng_constructor_t ng_vlan_constructor;
static ng_rcvmsg_t ng_vlan_rcvmsg;
static ng_shutdown_t ng_vlan_shutdown;
static ng_newhook_t ng_vlan_newhook;
static ng_rcvdata_t ng_vlan_rcvdata;
static ng_disconnect_t ng_vlan_disconnect;
/* Parse type for struct ng_vlan_filter. */
static const struct ng_parse_struct_field ng_vlan_filter_fields[] =
NG_VLAN_FILTER_FIELDS;
static const struct ng_parse_type ng_vlan_filter_type = {
&ng_parse_struct_type,
&ng_vlan_filter_fields
};
static int
ng_vlan_getTableLength(const struct ng_parse_type *type,
const u_char *start, const u_char *buf)
{
const struct ng_vlan_table *const table =
(const struct ng_vlan_table *)(buf - sizeof(u_int32_t));
return table->n;
}
/* Parse type for struct ng_vlan_table. */
static const struct ng_parse_array_info ng_vlan_table_array_info = {
&ng_vlan_filter_type,
ng_vlan_getTableLength
};
static const struct ng_parse_type ng_vlan_table_array_type = {
&ng_parse_array_type,
&ng_vlan_table_array_info
};
static const struct ng_parse_struct_field ng_vlan_table_fields[] =
NG_VLAN_TABLE_FIELDS;
static const struct ng_parse_type ng_vlan_table_type = {
&ng_parse_struct_type,
&ng_vlan_table_fields
};
/* List of commands and how to convert arguments to/from ASCII. */
static const struct ng_cmdlist ng_vlan_cmdlist[] = {
{
NGM_VLAN_COOKIE,
NGM_VLAN_ADD_FILTER,
"addfilter",
&ng_vlan_filter_type,
NULL
},
{
NGM_VLAN_COOKIE,
NGM_VLAN_DEL_FILTER,
"delfilter",
&ng_parse_hookbuf_type,
NULL
},
{
NGM_VLAN_COOKIE,
NGM_VLAN_GET_TABLE,
"gettable",
NULL,
&ng_vlan_table_type
},
{
NGM_VLAN_COOKIE,
NGM_VLAN_GET_ENCAP,
"getencap",
NULL,
&ng_parse_int32_type
},
{
NGM_VLAN_COOKIE,
NGM_VLAN_SET_ENCAP,
"setencap",
&ng_parse_int32_type,
NULL
},
{ 0 }
};
static struct ng_type ng_vlan_typestruct = {
.version = NG_ABI_VERSION,
.name = NG_VLAN_NODE_TYPE,
.constructor = ng_vlan_constructor,
.rcvmsg = ng_vlan_rcvmsg,
.shutdown = ng_vlan_shutdown,
.newhook = ng_vlan_newhook,
.rcvdata = ng_vlan_rcvdata,
.disconnect = ng_vlan_disconnect,
.cmdlist = ng_vlan_cmdlist,
};
NETGRAPH_INIT(vlan, &ng_vlan_typestruct);
typedef struct {
hook_p downstream_hook;
hook_p nomatch_hook;
hook_p vlan_hook[(EVL_VLID_MASK + 1)];
int vlan_encap;
} *priv_p;
#define HOOK_VLAN_SET_MASK ((uintptr_t)((~0) & ~(EVL_VLID_MASK)))
static int
ng_vlan_constructor(node_p node)
{
priv_p priv;
priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
if (priv == NULL)
return (ENOMEM);
priv->vlan_encap = 1;
return (0);
}
static int
ng_vlan_newhook(node_p node, hook_p hook, const char *name)
{
const priv_p priv = NG_NODE_PRIVATE(node);
if (strcmp(name, NG_VLAN_HOOK_DOWNSTREAM) == 0)
priv->downstream_hook = hook;
else if (strcmp(name, NG_VLAN_HOOK_NOMATCH) == 0)
priv->nomatch_hook = hook;
else {
/*
* Any other hook name is valid and can
* later be associated with a filter rule.
*/
}
NG_HOOK_SET_PRIVATE(hook, NULL);
return (0);
}
static int
ng_vlan_rcvmsg(node_p node, item_p item, hook_p lasthook)
{
const priv_p priv = NG_NODE_PRIVATE(node);
struct ng_mesg *msg, *resp = NULL;
struct ng_vlan_filter *vf;
hook_p hook;
struct ng_vlan_table *t;
uintptr_t vlan, vlan_count;
int error = 0;
NGI_GET_MSG(item, msg);
/* Deal with message according to cookie and command. */
switch (msg->header.typecookie) {
case NGM_VLAN_COOKIE:
switch (msg->header.cmd) {
case NGM_VLAN_ADD_FILTER:
/* Check that message is long enough. */
if (msg->header.arglen != sizeof(*vf)) {
error = EINVAL;
break;
}
vf = (struct ng_vlan_filter *)msg->data;
/* Sanity check the VLAN ID value. */
if (vf->vlan & ~EVL_VLID_MASK) {
error = EINVAL;
break;
}
/* Check that a referenced hook exists. */
hook = ng_findhook(node, vf->hook);
if (hook == NULL) {
error = ENOENT;
break;
}
/* And is not one of the special hooks. */
if (hook == priv->downstream_hook ||
hook == priv->nomatch_hook) {
error = EINVAL;
break;
}
/* And is not already in service. */
if (((uintptr_t)NG_HOOK_PRIVATE(hook) & HOOK_VLAN_SET_MASK) == HOOK_VLAN_SET_MASK) {
error = EEXIST;
break;
}
/* Check we don't already trap this VLAN. */
if (priv->vlan_hook[vf->vlan] != NULL) {
error = EEXIST;
break;
}
/* Link vlan and hook together. */
priv->vlan_hook[vf->vlan] = hook;
NG_HOOK_SET_PRIVATE(hook, (void *)(HOOK_VLAN_SET_MASK | vf->vlan));
break;
case NGM_VLAN_DEL_FILTER:
/* Check that message is long enough. */
if (msg->header.arglen != NG_HOOKSIZ) {
error = EINVAL;
break;
}
/* Check that hook exists and is active. */
hook = ng_findhook(node, (char *)msg->data);
if (hook == NULL)
error = ENOENT;
break;
vlan = (uintptr_t)NG_HOOK_PRIVATE(hook);
if ((vlan & HOOK_VLAN_SET_MASK) != HOOK_VLAN_SET_MASK) {
error = ENOENT;
break;
}
/* Purge a rule that refers to this hook. */
NG_HOOK_SET_PRIVATE(hook, NULL);
priv->vlan_hook[(vlan & EVL_VLID_MASK)] = NULL;
break;
case NGM_VLAN_GET_TABLE:
/* calculate vlans */
vlan_count = 0;
for (vlan = 0; vlan < (EVL_VLID_MASK + 1); vlan ++) {
if (priv->vlan_hook[vlan] != NULL)
vlan_count ++;
}
/* allocate memory for responce */
NG_MKRESPONSE(resp, msg, sizeof(*t) +
vlan_count * sizeof(*t->filter), M_NOWAIT);
if (resp == NULL) {
error = ENOMEM;
break;
}
/* pack data to responce */
t = (struct ng_vlan_table *)resp->data;
t->n = vlan_count;
vf = &t->filter[0];
for (vlan = 0; vlan < (EVL_VLID_MASK + 1); vlan ++) {
if (priv->vlan_hook[vlan] == NULL)
continue;
vf->vlan = vlan;
strncpy(vf->hook, NG_HOOK_NAME(priv->vlan_hook[vlan]),
NG_HOOKSIZ);
vf ++;
}
break;
case NGM_VLAN_GET_ENCAP:
NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
if (resp == NULL) {
error = ENOMEM;
break;
}
(*((u_int32_t *)resp->data)) = priv->vlan_encap;
break;
case NGM_VLAN_SET_ENCAP:
if (msg->header.arglen != sizeof(u_int32_t)) {
error = EINVAL;
break;
}
priv->vlan_encap = ((*((u_int32_t *)msg->data)) != 0);
break;
default: /* Unknown command. */
error = EINVAL;
break;
}
break;
case NGM_FLOW_COOKIE:
{
struct ng_mesg *copy;
/*
* Flow control messages should come only
* from downstream.
*/
if (lasthook == NULL)
break;
if (lasthook != priv->downstream_hook)
break;
/* Broadcast the event to all uplinks. */
for (vlan = 0; vlan < (EVL_VLID_MASK + 1); vlan ++) {
if (priv->vlan_hook[vlan] == NULL)
continue;
NG_COPYMESSAGE(copy, msg, M_NOWAIT);
if (copy == NULL)
continue;
NG_SEND_MSG_HOOK(error, node, copy, priv->vlan_hook[vlan], 0);
}
break;
}
default: /* Unknown type cookie. */
error = EINVAL;
break;
}
NG_RESPOND_MSG(error, node, item, resp);
NG_FREE_MSG(msg);
return (error);
}
static int
ng_vlan_rcvdata(hook_p hook, item_p item)
{
const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
struct ether_header *eh;
struct ether_vlan_header *evl = NULL;
int error;
uintptr_t vlan;
struct mbuf *m;
hook_p vlan_hook;
/* Make sure we have an entire header. */
NGI_GET_M(item, m);
if (m->m_len < sizeof(*eh) &&
(m = m_pullup(m, sizeof(*eh))) == NULL) {
NG_FREE_ITEM(item);
return (EINVAL);
}
eh = mtod(m, struct ether_header *);
if (hook == priv->downstream_hook) {
/*
* If from downstream, select between a match hook
* or the nomatch hook.
*/
vlan_hook = priv->nomatch_hook;
if (m->m_flags & M_VLANTAG ||
eh->ether_type == htons(ETHERTYPE_VLAN)) {
if (m->m_flags & M_VLANTAG) {
/*
* Packet is tagged, m contains a normal
* Ethernet frame; tag is stored out-of-band.
*/
vlan = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
} else {
if (m->m_len < sizeof(*evl) &&
(m = m_pullup(m, sizeof(*evl))) == NULL) {
NG_FREE_ITEM(item);
return (EINVAL);
}
evl = mtod(m, struct ether_vlan_header *);
vlan = EVL_VLANOFTAG(ntohs(evl->evl_tag));
}
if (priv->vlan_hook[vlan] != NULL) {
vlan_hook = priv->vlan_hook[vlan];
if (m->m_flags & M_VLANTAG) {
m->m_pkthdr.ether_vtag = 0;
m->m_flags &= ~M_VLANTAG;
} else {
evl->evl_encap_proto = evl->evl_proto;
bcopy(mtod(m, caddr_t),
mtod(m, caddr_t) +
ETHER_VLAN_ENCAP_LEN,
ETHER_HDR_LEN);
m_adj(m, ETHER_VLAN_ENCAP_LEN);
}
}
}
NG_FWD_NEW_DATA(error, item, vlan_hook, m);
} else {
/*
* It is heading towards the downstream.
* If from nomatch, pass it unmodified.
* Otherwise, do the VLAN encapsulation.
*/
if (hook != priv->nomatch_hook) {
vlan = (uintptr_t)NG_HOOK_PRIVATE(hook);
if ((vlan & HOOK_VLAN_SET_MASK) != HOOK_VLAN_SET_MASK) {
NG_FREE_ITEM(item);
NG_FREE_M(m);
return (EOPNOTSUPP);
}
if (priv->vlan_encap == 0) {
m->m_flags |= M_VLANTAG;
m->m_pkthdr.ether_vtag = (vlan & EVL_VLID_MASK);
} else {
M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT);
/* M_PREPEND takes care of m_len and m_pkthdr.len. */
if (m == NULL || (m->m_len < sizeof(*evl) &&
(m = m_pullup(m, sizeof(*evl))) == NULL)) {
NG_FREE_ITEM(item);
return (ENOMEM);
}
/*
* Transform the Ethernet header into an Ethernet header
* with 802.1Q encapsulation.
*/
evl = mtod(m, struct ether_vlan_header *);
bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
(char *)evl, (ETHER_HDR_LEN - ETHER_TYPE_LEN));
evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
evl->evl_tag = htons((vlan & EVL_VLID_MASK));
}
}
NG_FWD_NEW_DATA(error, item, priv->downstream_hook, m);
}
return (error);
}
static int
ng_vlan_shutdown(node_p node)
{
const priv_p priv = NG_NODE_PRIVATE(node);
NG_NODE_SET_PRIVATE(node, NULL);
NG_NODE_UNREF(node);
free(priv, M_NETGRAPH);
return (0);
}
static int
ng_vlan_disconnect(hook_p hook)
{
const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
uintptr_t vlan;
if (hook == priv->downstream_hook)
priv->downstream_hook = NULL;
else if (hook == priv->nomatch_hook)
priv->nomatch_hook = NULL;
else {
/* Purge a rule that refers to this hook. */
vlan = (uintptr_t)NG_HOOK_PRIVATE(hook);
if ((vlan & HOOK_VLAN_SET_MASK) == HOOK_VLAN_SET_MASK)
priv->vlan_hook[(vlan & EVL_VLID_MASK)] = NULL;
}
NG_HOOK_SET_PRIVATE(hook, NULL);
if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) &&
(NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
ng_rmnode_self(NG_HOOK_NODE(hook));
return (0);
}
-------------- next part --------------
/*-
* Copyright (c) 2003 IPNET Internet Communication Company
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Author: Ruslan Ermilov <ru at FreeBSD.org>
*
* $FreeBSD: src/sys/netgraph/ng_vlan.h,v 1.2.10.1 2009/08/03 08:13:06 kensmith Exp $
*/
#ifndef _NETGRAPH_NG_VLAN_H_
#define _NETGRAPH_NG_VLAN_H_
/* Node type name and magic cookie. */
#define NG_VLAN_NODE_TYPE "vlan"
#define NGM_VLAN_COOKIE 1068486472
/* Hook names. */
#define NG_VLAN_HOOK_DOWNSTREAM "downstream"
#define NG_VLAN_HOOK_NOMATCH "nomatch"
/* Netgraph commands. */
enum {
NGM_VLAN_ADD_FILTER = 1,
NGM_VLAN_DEL_FILTER,
NGM_VLAN_GET_TABLE,
NGM_VLAN_GET_ENCAP,
NGM_VLAN_SET_ENCAP
};
/* For NGM_VLAN_ADD_FILTER control message. */
struct ng_vlan_filter {
char hook[NG_HOOKSIZ];
u_int16_t vlan;
};
/* Keep this in sync with the above structure definition. */
#define NG_VLAN_FILTER_FIELDS { \
{ "hook", &ng_parse_hookbuf_type }, \
{ "vlan", &ng_parse_uint16_type }, \
{ NULL } \
}
/* Structure returned by NGM_VLAN_GET_TABLE. */
struct ng_vlan_table {
u_int32_t n;
struct ng_vlan_filter filter[];
};
/* Keep this in sync with the above structure definition. */
#define NG_VLAN_TABLE_FIELDS { \
{ "n", &ng_parse_uint32_type }, \
{ "filter", &ng_vlan_table_array_type }, \
{ NULL } \
}
#endif /* _NETGRAPH_NG_VLAN_H_ */
More information about the freebsd-net
mailing list