2 * Copyright (c) 2003 IPNET Internet Communication Company
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * Author: Ruslan Ermilov <ru@FreeBSD.org>
31 #include <sys/param.h>
32 #include <sys/errno.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
36 #include <sys/queue.h>
37 #include <sys/socket.h>
38 #include <sys/systm.h>
40 #include <net/ethernet.h>
42 #include <net/if_vlan_var.h>
44 #include <netgraph/ng_message.h>
45 #include <netgraph/ng_parse.h>
46 #include <netgraph/ng_vlan.h>
47 #include <netgraph/netgraph.h>
49 static ng_constructor_t ng_vlan_constructor;
50 static ng_rcvmsg_t ng_vlan_rcvmsg;
51 static ng_shutdown_t ng_vlan_shutdown;
52 static ng_newhook_t ng_vlan_newhook;
53 static ng_rcvdata_t ng_vlan_rcvdata;
54 static ng_disconnect_t ng_vlan_disconnect;
56 /* Parse type for struct ng_vlan_filter. */
57 static const struct ng_parse_struct_field ng_vlan_filter_fields[] =
58 NG_VLAN_FILTER_FIELDS;
59 static const struct ng_parse_type ng_vlan_filter_type = {
60 &ng_parse_struct_type,
61 &ng_vlan_filter_fields
65 ng_vlan_getTableLength(const struct ng_parse_type *type,
66 const u_char *start, const u_char *buf)
68 const struct ng_vlan_table *const table =
69 (const struct ng_vlan_table *)(buf - sizeof(u_int32_t));
74 /* Parse type for struct ng_vlan_table. */
75 static const struct ng_parse_array_info ng_vlan_table_array_info = {
77 ng_vlan_getTableLength
79 static const struct ng_parse_type ng_vlan_table_array_type = {
81 &ng_vlan_table_array_info
83 static const struct ng_parse_struct_field ng_vlan_table_fields[] =
85 static const struct ng_parse_type ng_vlan_table_type = {
86 &ng_parse_struct_type,
90 /* List of commands and how to convert arguments to/from ASCII. */
91 static const struct ng_cmdlist ng_vlan_cmdlist[] = {
103 &ng_parse_hookbuf_type,
116 static struct ng_type ng_vlan_typestruct = {
117 .version = NG_ABI_VERSION,
118 .name = NG_VLAN_NODE_TYPE,
119 .constructor = ng_vlan_constructor,
120 .rcvmsg = ng_vlan_rcvmsg,
121 .shutdown = ng_vlan_shutdown,
122 .newhook = ng_vlan_newhook,
123 .rcvdata = ng_vlan_rcvdata,
124 .disconnect = ng_vlan_disconnect,
125 .cmdlist = ng_vlan_cmdlist,
127 NETGRAPH_INIT(vlan, &ng_vlan_typestruct);
130 LIST_ENTRY(filter) next;
136 #define HASH(id) ((((id) >> 8) ^ ((id) >> 4) ^ (id)) & 0x0f)
137 LIST_HEAD(filterhead, filter);
140 hook_p downstream_hook;
142 struct filterhead hashtable[HASHSIZE];
146 static struct filter *
147 ng_vlan_findentry(priv_p priv, u_int16_t vlan)
149 struct filterhead *chain = &priv->hashtable[HASH(vlan)];
152 LIST_FOREACH(f, chain, next)
159 ng_vlan_constructor(node_p node)
164 priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
165 for (i = 0; i < HASHSIZE; i++)
166 LIST_INIT(&priv->hashtable[i]);
167 NG_NODE_SET_PRIVATE(node, priv);
172 ng_vlan_newhook(node_p node, hook_p hook, const char *name)
174 const priv_p priv = NG_NODE_PRIVATE(node);
176 if (strcmp(name, NG_VLAN_HOOK_DOWNSTREAM) == 0)
177 priv->downstream_hook = hook;
178 else if (strcmp(name, NG_VLAN_HOOK_NOMATCH) == 0)
179 priv->nomatch_hook = hook;
182 * Any other hook name is valid and can
183 * later be associated with a filter rule.
186 NG_HOOK_SET_PRIVATE(hook, NULL);
191 ng_vlan_rcvmsg(node_p node, item_p item, hook_p lasthook)
193 const priv_p priv = NG_NODE_PRIVATE(node);
195 struct ng_mesg *msg, *resp = NULL;
196 struct ng_vlan_filter *vf;
199 struct ng_vlan_table *t;
202 NGI_GET_MSG(item, msg);
203 /* Deal with message according to cookie and command. */
204 switch (msg->header.typecookie) {
205 case NGM_VLAN_COOKIE:
206 switch (msg->header.cmd) {
207 case NGM_VLAN_ADD_FILTER:
208 /* Check that message is long enough. */
209 if (msg->header.arglen != sizeof(*vf)) {
213 vf = (struct ng_vlan_filter *)msg->data;
214 /* Sanity check the VLAN ID value. */
215 if (vf->vlan & ~EVL_VLID_MASK) {
219 /* Check that a referenced hook exists. */
220 hook = ng_findhook(node, vf->hook);
225 /* And is not one of the special hooks. */
226 if (hook == priv->downstream_hook ||
227 hook == priv->nomatch_hook) {
231 /* And is not already in service. */
232 if (NG_HOOK_PRIVATE(hook) != NULL) {
236 /* Check we don't already trap this VLAN. */
237 if (ng_vlan_findentry(priv, vf->vlan)) {
242 f = malloc(sizeof(*f),
243 M_NETGRAPH, M_NOWAIT | M_ZERO);
248 /* Link filter and hook together. */
251 NG_HOOK_SET_PRIVATE(hook, f);
252 /* Register filter in a hash table. */
254 &priv->hashtable[HASH(f->vlan)], f, next);
257 case NGM_VLAN_DEL_FILTER:
258 /* Check that message is long enough. */
259 if (msg->header.arglen != NG_HOOKSIZ) {
263 /* Check that hook exists and is active. */
264 hook = ng_findhook(node, (char *)msg->data);
266 (f = NG_HOOK_PRIVATE(hook)) == NULL) {
270 /* Purge a rule that refers to this hook. */
271 NG_HOOK_SET_PRIVATE(hook, NULL);
272 LIST_REMOVE(f, next);
276 case NGM_VLAN_GET_TABLE:
277 NG_MKRESPONSE(resp, msg, sizeof(*t) +
278 priv->nent * sizeof(*t->filter), M_NOWAIT);
283 t = (struct ng_vlan_table *)resp->data;
286 for (i = 0; i < HASHSIZE; i++) {
287 LIST_FOREACH(f, &priv->hashtable[i], next) {
289 strncpy(vf->hook, NG_HOOK_NAME(f->hook),
295 default: /* Unknown command. */
300 case NGM_FLOW_COOKIE:
302 struct ng_mesg *copy;
303 struct filterhead *chain;
307 * Flow control messages should come only
311 if (lasthook == NULL)
313 if (lasthook != priv->downstream_hook)
316 /* Broadcast the event to all uplinks. */
317 for (i = 0, chain = priv->hashtable; i < HASHSIZE;
319 LIST_FOREACH(f, chain, next) {
320 NG_COPYMESSAGE(copy, msg, M_NOWAIT);
323 NG_SEND_MSG_HOOK(error, node, copy, f->hook, 0);
328 default: /* Unknown type cookie. */
332 NG_RESPOND_MSG(error, node, item, resp);
338 ng_vlan_rcvdata(hook_p hook, item_p item)
340 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
341 struct ether_header *eh;
342 struct ether_vlan_header *evl = NULL;
348 /* Make sure we have an entire header. */
350 if (m->m_len < sizeof(*eh) &&
351 (m = m_pullup(m, sizeof(*eh))) == NULL) {
355 eh = mtod(m, struct ether_header *);
356 if (hook == priv->downstream_hook) {
358 * If from downstream, select between a match hook
359 * or the nomatch hook.
361 if (m->m_flags & M_VLANTAG ||
362 eh->ether_type == htons(ETHERTYPE_VLAN)) {
363 if (m->m_flags & M_VLANTAG) {
365 * Packet is tagged, m contains a normal
366 * Ethernet frame; tag is stored out-of-band.
368 vlan = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
370 if (m->m_len < sizeof(*evl) &&
371 (m = m_pullup(m, sizeof(*evl))) == NULL) {
375 evl = mtod(m, struct ether_vlan_header *);
376 vlan = EVL_VLANOFTAG(ntohs(evl->evl_tag));
378 if ((f = ng_vlan_findentry(priv, vlan)) != NULL) {
379 if (m->m_flags & M_VLANTAG) {
380 m->m_pkthdr.ether_vtag = 0;
381 m->m_flags &= ~M_VLANTAG;
383 evl->evl_encap_proto = evl->evl_proto;
384 bcopy(mtod(m, caddr_t),
386 ETHER_VLAN_ENCAP_LEN,
388 m_adj(m, ETHER_VLAN_ENCAP_LEN);
394 NG_FWD_NEW_DATA(error, item, f->hook, m);
396 NG_FWD_NEW_DATA(error, item, priv->nomatch_hook, m);
399 * It is heading towards the downstream.
400 * If from nomatch, pass it unmodified.
401 * Otherwise, do the VLAN encapsulation.
403 if (hook != priv->nomatch_hook) {
404 if ((f = NG_HOOK_PRIVATE(hook)) == NULL) {
409 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT);
410 /* M_PREPEND takes care of m_len and m_pkthdr.len. */
411 if (m == NULL || (m->m_len < sizeof(*evl) &&
412 (m = m_pullup(m, sizeof(*evl))) == NULL)) {
417 * Transform the Ethernet header into an Ethernet header
418 * with 802.1Q encapsulation.
420 bcopy(mtod(m, char *) + ETHER_VLAN_ENCAP_LEN,
421 mtod(m, char *), ETHER_HDR_LEN);
422 evl = mtod(m, struct ether_vlan_header *);
423 evl->evl_proto = evl->evl_encap_proto;
424 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
425 evl->evl_tag = htons(f->vlan);
427 NG_FWD_NEW_DATA(error, item, priv->downstream_hook, m);
433 ng_vlan_shutdown(node_p node)
435 const priv_p priv = NG_NODE_PRIVATE(node);
437 NG_NODE_SET_PRIVATE(node, NULL);
439 free(priv, M_NETGRAPH);
444 ng_vlan_disconnect(hook_p hook)
446 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
449 if (hook == priv->downstream_hook)
450 priv->downstream_hook = NULL;
451 else if (hook == priv->nomatch_hook)
452 priv->nomatch_hook = NULL;
454 /* Purge a rule that refers to this hook. */
455 if ((f = NG_HOOK_PRIVATE(hook)) != NULL) {
456 LIST_REMOVE(f, next);
461 NG_HOOK_SET_PRIVATE(hook, NULL);
462 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) &&
463 (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
464 ng_rmnode_self(NG_HOOK_NODE(hook));