summaryrefslogtreecommitdiff
path: root/polux/application/ebtables/extensions/ebt_vlan_t.c
diff options
context:
space:
mode:
Diffstat (limited to 'polux/application/ebtables/extensions/ebt_vlan_t.c')
-rw-r--r--polux/application/ebtables/extensions/ebt_vlan_t.c145
1 files changed, 145 insertions, 0 deletions
diff --git a/polux/application/ebtables/extensions/ebt_vlan_t.c b/polux/application/ebtables/extensions/ebt_vlan_t.c
new file mode 100644
index 0000000000..675813318c
--- /dev/null
+++ b/polux/application/ebtables/extensions/ebt_vlan_t.c
@@ -0,0 +1,145 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <getopt.h>
+#include "../include/ebtables_u.h"
+#include <linux/netfilter_bridge/ebt_vlan_t.h>
+
+#define VLAN_TARGET '1'
+#define VLAN_ENCAPS_VID '2'
+#define VLAN_ENCAPS_PRIO '3'
+#define VLAN_DECAPS '4'
+static struct option opts[] =
+ {
+ { "vlan-decaps" , no_argument , 0, VLAN_DECAPS },
+ { "vlan-encaps-prio" , required_argument, 0, VLAN_ENCAPS_PRIO },
+ { "vlan-encaps-vid" , required_argument, 0, VLAN_ENCAPS_VID },
+ { "vlan-target" , required_argument, 0, VLAN_TARGET },
+ { 0 }
+ };
+
+static void print_help()
+{
+ printf(
+ "vlan target options:\n"
+ " --vlan-decaps : remove VLAN header if exists\n"
+ " --vlan-encaps-prio prio : add a VLAN header with new prio (or change it if exists)\n"
+ " --vlan-encaps-vid vid : add a VLAN header with new vid (or change it if exists)\n"
+ " --vlan-target target : ACCEPT, DROP, RETURN or CONTINUE\n");
+}
+
+static void init(struct ebt_entry_target *target)
+{
+ struct ebt_vlan_t_info *vlaninfo =
+ (struct ebt_vlan_t_info *)target->data;
+
+ vlaninfo->target = EBT_ACCEPT;
+ vlaninfo->prio = 0;
+ vlaninfo->vid = 0;
+ vlaninfo->action = 0;
+}
+
+#define OPT_VLAN_TARGET 0x01
+#define OPT_VLAN_DECAPS 0x02
+#define OPT_VLAN_ENCAPS_PRIO 0x04
+#define OPT_VLAN_ENCAPS_VID 0x08
+static int parse(int c, char **argv, int argc,
+ const struct ebt_u_entry *entry, unsigned int *flags,
+ struct ebt_entry_target **target)
+{
+ struct ebt_vlan_t_info *vlaninfo =
+ (struct ebt_vlan_t_info *)(*target)->data;
+ char *end;
+
+ switch (c) {
+ case VLAN_TARGET:
+ check_option(flags, OPT_VLAN_TARGET);
+ if (FILL_TARGET(optarg, vlaninfo->target))
+ print_error("Illegal --vlan-target target");
+ break;
+ case VLAN_DECAPS:
+ check_option(flags, OPT_VLAN_DECAPS);
+ vlaninfo->action |= EBT_VLAN_ACTION_DECAPS;
+ break;
+ case VLAN_ENCAPS_PRIO:
+ check_option (flags, OPT_VLAN_ENCAPS_PRIO);
+ vlaninfo->prio = strtoul(optarg, &end, 0);
+ if (*end != '\0' || vlaninfo->prio < 0 || vlaninfo->prio > 7)
+ print_error("Bad VLAN PRIO '%s'", optarg);
+ vlaninfo->action |= EBT_VLAN_ACTION_ENCAPS_PRIO;
+ break;
+ case VLAN_ENCAPS_VID:
+ check_option (flags, OPT_VLAN_ENCAPS_VID);
+ vlaninfo->vid = strtoul(optarg, &end, 0);
+ if (*end != '\0' || vlaninfo->vid < 0 || vlaninfo->vid > 4095)
+ print_error("Bad VLAN VID '%s'", optarg);
+ vlaninfo->action |= EBT_VLAN_ACTION_ENCAPS_VID;
+ break;
+ default:
+ return 0;
+ }
+ return 1;
+}
+
+static void final_check(const struct ebt_u_entry *entry,
+ const struct ebt_entry_target *target, const char *name,
+ unsigned int hookmask, unsigned int time)
+{
+ struct ebt_vlan_t_info *vlaninfo =
+ (struct ebt_vlan_t_info *)target->data;
+
+ if ((vlaninfo->action & EBT_VLAN_ACTION_DECAPS) &&
+ (vlaninfo->action & (EBT_VLAN_ACTION_ENCAPS_VID | EBT_VLAN_ACTION_ENCAPS_VID)))
+ print_error ("--vlan-decaps and --vlan-encaps-* are exclusive");
+
+ if (BASE_CHAIN && vlaninfo->target == EBT_RETURN)
+ print_error("--vlan-target RETURN not allowed on base chain");
+}
+
+static void print(const struct ebt_u_entry *entry,
+ const struct ebt_entry_target *target)
+{
+ struct ebt_vlan_t_info *vlaninfo =
+ (struct ebt_vlan_t_info *)target->data;
+
+ if(vlaninfo->action & EBT_VLAN_ACTION_DECAPS)
+ printf ("--vlan-decaps ");
+ if(vlaninfo->action & EBT_VLAN_ACTION_ENCAPS_PRIO)
+ printf ("--vlan-encaps-prio 0x%d ", vlaninfo->prio);
+ if(vlaninfo->action & EBT_VLAN_ACTION_ENCAPS_VID)
+ printf ("--vlan-encaps-vid 0x%d ", vlaninfo->vid);
+ if (vlaninfo->target == EBT_ACCEPT)
+ return;
+ printf("--vlan-target %s", TARGET_NAME(vlaninfo->target));
+}
+
+static int compare(const struct ebt_entry_target *t1,
+ const struct ebt_entry_target *t2)
+{
+ struct ebt_vlan_t_info *vlaninfo1 = (struct ebt_vlan_t_info *)t1->data;
+ struct ebt_vlan_t_info *vlaninfo2 = (struct ebt_vlan_t_info *)t2->data;
+
+ return vlaninfo1->target == vlaninfo2->target &&
+ vlaninfo1->action == vlaninfo2->action &&
+ vlaninfo1->prio == vlaninfo2->prio &&
+ vlaninfo1->vid == vlaninfo2->vid;
+}
+
+static struct ebt_u_target vlan_target =
+ {
+ .name = EBT_VLAN_TARGET,
+ .size = sizeof(struct ebt_vlan_t_info),
+ .help = print_help,
+ .init = init,
+ .parse = parse,
+ .final_check = final_check,
+ .print = print,
+ .compare = compare,
+ .extra_ops = opts,
+ };
+
+//static void _init(void) __attribute__ ((constructor));
+void ebt_vlan_t_init(void)
+{
+ register_target(&vlan_target);
+}