vlan target for iptables
Subsystems (2)
Please consider this for inclusion in iptables. Should this go in pom?
This adds --vlan target matching for iptables. Useful when running as a bridge and perhaps in other cases.
I've tested it while bridging packets with vlan headers set. Anyone trying to use this, realise that in most configurations your switch does not transmit the vlan headers, and if it does, then realise that your networked PC's will not generally accept the packets. The test rig involved a few PC's and some segmented switches.
I'm afraid I had to steal the vlan packet matching code IS_VLAN_* from one of the vlan modules as it was private to that module. I hate having to do that.
For user-space:
Just add dump libipt_vlan.c extensions and add vlan to PF_EXT_SLIB definition in extensions/Makefile
I'm not sending a patch for extensions/Makefile that because it's bound to fail applying with all the PF_EXT_SLIB stuff all on one line.
For kernel-space, apply vlan.patch
Comments? This code is based on the MAC target with minimal changes (and careful "grep -i mac *" and is naturally GPL licensed too),
Azez
/* Shared library add-on to iptables to add VLAN address support. */ #include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#if defined(__GLIBC__) && __GLIBC__ == 2
#include <net/ethernet.h>
#else
#include <linux/if_ether.h>
#endif
#include <iptables.h>
#include <linux/netfilter_ipv4/ipt_vlan.h>
/* Function which prints out usage message. */
static void
help(void)
{
printf("VLAN v%s options:\n"
" --vlan [!] <vlan_id>\n"
" Match source VLAN id\n"
"\n", IPTABLES_VERSION);
}
static struct option opts[] = {
{ "vlan", 1, 0, '1' },
{0}
};/* Function which parses command options; returns true if it
ate an option */
static int
parse(int c, char **argv, int invert, unsigned int *flags,
const struct ipt_entry *entry,
unsigned int *nfcache,
struct ipt_entry_match **match)
{
struct ipt_vlan_info *vlaninfo = (struct ipt_vlan_info *)(*match)->data;
switch (c) {
case '1':
check_inverse(optarg, &invert, &optind, 0);
vlaninfo->vlan=atoi(argv[optind-1]);
if (invert)
vlaninfo->invert = 1;
*flags = 1;
break;
default:
return 0;
}
return 1;
}/* Final check; must have specified --vlan. */
static void final_check(unsigned int flags)
{
if (!flags)
exit_error(PARAMETER_PROBLEM,
"You must specify `--vlan'");
}/* Prints out the matchinfo. */
static void
print(const struct ipt_ip *ip,
const struct ipt_entry_match *match,
int numeric)
{
printf("vlan ");
if (((struct ipt_vlan_info *)match->data)->invert)
printf("! ");
printf("%d ",((struct ipt_vlan_info *)match->data)->vlan);
}/* Saves the union ipt_matchinfo in parsable form to stdout. */ static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
{
if (((struct ipt_vlan_info *)match->data)->invert)
printf("! ");
printf("--vlan %d ",((struct ipt_vlan_info *)match->data)->vlan);
}static struct iptables_match vlan = {
.next = NULL, .name = "vlan",
.version = IPTABLES_VERSION,
.size = IPT_ALIGN(sizeof(struct ipt_vlan_info)),
.userspacesize = IPT_ALIGN(sizeof(struct ipt_vlan_info)),
.help = &help,
.parse = &parse,
.final_check = &final_check,
.print = &print,
.save = &save,
.extra_opts = opts
};
void _init(void)
{
register_match(&vlan);
}include/linux/netfilter_ipv4/ipt_vlan.h renamed from linux-2.6.11.7-reference/include/linux/netfilter_ipv4/ipt_vlan.h +8 -0
@@ -0,0 +1,8 @@ +#ifndef _IPT_VLAN_H +#define _IPT_VLAN_H + +struct ipt_vlan_info { +unsigned short vlan; +int invert; +}; +#endif /*_IPT_VLAN_H*/
net/ipv4/netfilter/ipt_vlan.c renamed from linux-2.6.11.7-reference/net/ipv4/netfilter/ipt_vlan.c +82 -0
82 lines not on this page · raw message
net/ipv4/netfilter/Makefile renamed from net/ipv4/netfilter/Makefile.orig +1 -0
@@ -45,6 +45,7 @@ obj-$(CONFIG_IP_NF_MATCH_SCTP) += ipt_sctp.o obj-$(CONFIG_IP_NF_MATCH_MARK) += ipt_mark.o obj-$(CONFIG_IP_NF_MATCH_MAC) += ipt_mac.o +obj-$(CONFIG_IP_NF_MATCH_VLAN) += ipt_vlan.o obj-$(CONFIG_IP_NF_MATCH_IPRANGE) += ipt_iprange.o obj-$(CONFIG_IP_NF_MATCH_PKTTYPE) += ipt_pkttype.o obj-$(CONFIG_IP_NF_MATCH_MULTIPORT) += ipt_multiport.o
net/ipv4/netfilter/Kconfig renamed from net/ipv4/netfilter/Kconfig.orig +9 -0
@@ -183,6 +183,15 @@ Unless you know what you're doing, leave it at the default of 2kB. +config IP_NF_MATCH_VLAN + tristate "VLAN address match support" + depends on IP_NF_IPTABLES + help + VLAN matching allows you to match packets based on the vlan + tag of the packet, if your switch fowards them + + To compile it as a module, choose M here. If unsure, say N. + config IP_NF_MATCH_PKTTYPE tristate "Packet type match support" depends on IP_NF_IPTABLES
Am I supposed to do anything particular to get this included with iptables?
I know pom is going out of the window.
Azez
Quoted 24 lines · Amin Azez
Please consider this for inclusion in iptables. Should this go in pom?
This adds --vlan target matching for iptables. Useful when running as a bridge and perhaps in other cases.
I've tested it while bridging packets with vlan headers set. Anyone trying to use this, realise that in most configurations your switch does not transmit the vlan headers, and if it does, then realise that your networked PC's will not generally accept the packets. The test rig involved a few PC's and some segmented switches.
I'm afraid I had to steal the vlan packet matching code IS_VLAN_* from one of the vlan modules as it was private to that module. I hate having to do that.
For user-space:
Just add dump
libipt_vlan.cextensions and add vlan toPF_EXT_SLIBdefinition in extensions/MakefileI'm not sending a patch for extensions/Makefile that because it's bound to fail applying with all the
PF_EXT_SLIBstuff all on one line.
Quoted 6 lines
For kernel-space, apply vlan.patch
Comments? This code is based on the MAC target with minimal changes (and careful "grep -i mac *" and is naturally GPL licensed too),
Azez
/* Shared library add-on to iptables to add VLAN address support. */ #include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#if defined(__GLIBC__) && __GLIBC__ == 2
#include <net/ethernet.h>
#else
#include <linux/if_ether.h>
#endif
#include <iptables.h>
#include <linux/netfilter_ipv4/ipt_vlan.h>
/* Function which prints out usage message. */
static void
help(void)
{
printf("VLAN v%s options:\n"
" --vlan [!] <vlan_id>\n"
" Match source VLAN id\n"
"\n", IPTABLES_VERSION);
}
static struct option opts[] = {
{ "vlan", 1, 0, '1' },
{0}
};/* Function which parses command options; returns true if it
ate an option */
static int
parse(int c, char **argv, int invert, unsigned int *flags,
const struct ipt_entry *entry,
unsigned int *nfcache,
struct ipt_entry_match **match)
{
struct ipt_vlan_info *vlaninfo = (struct ipt_vlan_info *)(*match)->data;
switch (c) {
case '1':
check_inverse(optarg, &invert, &optind, 0);
vlaninfo->vlan=atoi(argv[optind-1]);
if (invert)
vlaninfo->invert = 1;
*flags = 1;
break;
default:
return 0;
}
return 1;
}/* Final check; must have specified --vlan. */
static void final_check(unsigned int flags)
{
if (!flags)
exit_error(PARAMETER_PROBLEM,
"You must specify `--vlan'");
}/* Prints out the matchinfo. */
static void
print(const struct ipt_ip *ip,
const struct ipt_entry_match *match,
int numeric)
{
printf("vlan ");
if (((struct ipt_vlan_info *)match->data)->invert)
printf("! ");
printf("%d ",((struct ipt_vlan_info *)match->data)->vlan);
}/* Saves the union ipt_matchinfo in parsable form to stdout. */ static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
{
if (((struct ipt_vlan_info *)match->data)->invert)
printf("! ");
printf("--vlan %d ",((struct ipt_vlan_info *)match->data)->vlan);
}static struct iptables_match vlan = {
.next = NULL, .name = "vlan",
.version = IPTABLES_VERSION,
.size = IPT_ALIGN(sizeof(struct ipt_vlan_info)),
.userspacesize = IPT_ALIGN(sizeof(struct ipt_vlan_info)),
.help = &help,
.parse = &parse,
.final_check = &final_check,
.print = &print,
.save = &save,
.extra_opts = opts
};
void _init(void)
{
register_match(&vlan);
}include/linux/netfilter_ipv4/ipt_vlan.h renamed from linux-2.6.11.7-reference/include/linux/netfilter_ipv4/ipt_vlan.h +8 -0
@@ -0,0 +1,8 @@ +#ifndef _IPT_VLAN_H +#define _IPT_VLAN_H + +struct ipt_vlan_info { +unsigned short vlan; +int invert; +}; +#endif /*_IPT_VLAN_H*/
net/ipv4/netfilter/ipt_vlan.c renamed from linux-2.6.11.7-reference/net/ipv4/netfilter/ipt_vlan.c +82 -0
82 lines not on this page · raw message
net/ipv4/netfilter/Makefile renamed from net/ipv4/netfilter/Makefile.orig +1 -0
@@ -45,6 +45,7 @@ obj-$(CONFIG_IP_NF_MATCH_SCTP) += ipt_sctp.o obj-$(CONFIG_IP_NF_MATCH_MARK) += ipt_mark.o obj-$(CONFIG_IP_NF_MATCH_MAC) += ipt_mac.o +obj-$(CONFIG_IP_NF_MATCH_VLAN) += ipt_vlan.o obj-$(CONFIG_IP_NF_MATCH_IPRANGE) += ipt_iprange.o obj-$(CONFIG_IP_NF_MATCH_PKTTYPE) += ipt_pkttype.o obj-$(CONFIG_IP_NF_MATCH_MULTIPORT) += ipt_multiport.o
net/ipv4/netfilter/Kconfig renamed from net/ipv4/netfilter/Kconfig.orig +9 -0
@@ -183,6 +183,15 @@ Unless you know what you're doing, leave it at the default of 2kB. +config IP_NF_MATCH_VLAN + tristate "VLAN address match support" + depends on IP_NF_IPTABLES + help + VLAN matching allows you to match packets based on the vlan + tag of the packet, if your switch fowards them + + To compile it as a module, choose M here. If unsure, say N. + config IP_NF_MATCH_PKTTYPE tristate "Packet type match support" depends on IP_NF_IPTABLES
Quoted 1 line · Amin Azez
Am I supposed to do anything particular to get this included with iptables?
Netfiflter/iptables is a layer 3+ filter. VLAN is a layer 2 concept that Netfilter is not aware of.
BTW, you can use ebtables, wich is a layer 2 filter, to match VLANs.
http://ebtables.sourceforge.net/
Signature · 2 lines
http://sid.rstack.org/ PGP KeyID: 157E98EE FingerPrint: FA62226DA9E72FA8AECAA240008B480E157E98EE
Quoted 2 lines
Quoted 2 lines
Hi! I'm your friendly neighbourhood signature virus. Copy me to your signature file and help me spread!
Quoted 1 line · Amin Azez
Please consider this for inclusion in iptables. Should this go in pom?
I would merge it, once I'm sure that it is safe to use ;)
Quoted 7 lines
This adds --vlan target matching for iptables. Useful when running as a bridge and perhaps in other cases.
I'm afraid I had to steal the vlan packet matching code IS_VLAN_* from one of the vlan modules as it was private to that module. I hate having to do that.
Well, it actually makes sense since you cannot know whether the local network stack actually has vlan support (and therefore has those functions for vlan matching).
Quoted 7 lines
For user-space:
Just add dump
libipt_vlan.cextensions and add vlan toPF_EXT_SLIBdefinition in extensions/MakefileI'm not sending a patch for extensions/Makefile that because it's bound to fail applying with all the
PF_EXT_SLIBstuff all on one line.
The question is:
What happens with packets that don't have a vlan tag?
In order to alway work, it has to be sure that
1) you can match on all valid vlan tag numbers plus "NONE" for no tag
2) the kernel code produces reasonable results, i.e. also cleanly deals with (and matches) packets that don't have a vlan tag.
Cheers,
Harald
Signature · 5 lines
- Harald Welte <[email protected]> http://netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie