xt_physdev.c 5.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/* Kernel module to match the bridge port in and
 * out device for IP packets coming into contact with a bridge. */

/* (C) 2001-2003 Bart De Schuymer <bdschuym@pandora.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/skbuff.h>
13
#include <linux/netfilter_bridge.h>
14 15
#include <linux/netfilter/xt_physdev.h>
#include <linux/netfilter/x_tables.h>
L
Linus Torvalds 已提交
16 17 18 19 20 21 22
#include <linux/netfilter_bridge.h>
#define MATCH   1
#define NOMATCH 0

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
MODULE_DESCRIPTION("iptables bridge physical device match module");
23 24
MODULE_ALIAS("ipt_physdev");
MODULE_ALIAS("ip6t_physdev");
L
Linus Torvalds 已提交
25 26 27 28 29

static int
match(const struct sk_buff *skb,
      const struct net_device *in,
      const struct net_device *out,
30
      const struct xt_match *match,
L
Linus Torvalds 已提交
31 32 33 34 35 36 37
      const void *matchinfo,
      int offset,
      unsigned int protoff,
      int *hotdrop)
{
	int i;
	static const char nulldevname[IFNAMSIZ];
38
	const struct xt_physdev_info *info = matchinfo;
L
Linus Torvalds 已提交
39 40 41 42 43 44 45 46 47
	unsigned int ret;
	const char *indev, *outdev;
	struct nf_bridge_info *nf_bridge;

	/* Not a bridged IP packet or no info available yet:
	 * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if
	 * the destination device will be a bridge. */
	if (!(nf_bridge = skb->nf_bridge)) {
		/* Return MATCH if the invert flags of the used options are on */
48 49
		if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
		    !(info->invert & XT_PHYSDEV_OP_BRIDGED))
L
Linus Torvalds 已提交
50
			return NOMATCH;
51 52
		if ((info->bitmask & XT_PHYSDEV_OP_ISIN) &&
		    !(info->invert & XT_PHYSDEV_OP_ISIN))
L
Linus Torvalds 已提交
53
			return NOMATCH;
54 55
		if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) &&
		    !(info->invert & XT_PHYSDEV_OP_ISOUT))
L
Linus Torvalds 已提交
56
			return NOMATCH;
57 58
		if ((info->bitmask & XT_PHYSDEV_OP_IN) &&
		    !(info->invert & XT_PHYSDEV_OP_IN))
L
Linus Torvalds 已提交
59
			return NOMATCH;
60 61
		if ((info->bitmask & XT_PHYSDEV_OP_OUT) &&
		    !(info->invert & XT_PHYSDEV_OP_OUT))
L
Linus Torvalds 已提交
62 63 64 65 66
			return NOMATCH;
		return MATCH;
	}

	/* This only makes sense in the FORWARD and POSTROUTING chains */
67
	if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
L
Linus Torvalds 已提交
68
	    (!!(nf_bridge->mask & BRNF_BRIDGED) ^
69
	    !(info->invert & XT_PHYSDEV_OP_BRIDGED)))
L
Linus Torvalds 已提交
70 71
		return NOMATCH;

72 73 74 75
	if ((info->bitmask & XT_PHYSDEV_OP_ISIN &&
	    (!nf_bridge->physindev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) ||
	    (info->bitmask & XT_PHYSDEV_OP_ISOUT &&
	    (!nf_bridge->physoutdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT))))
L
Linus Torvalds 已提交
76 77
		return NOMATCH;

78
	if (!(info->bitmask & XT_PHYSDEV_OP_IN))
L
Linus Torvalds 已提交
79 80 81 82 83 84 85 86
		goto match_outdev;
	indev = nf_bridge->physindev ? nf_bridge->physindev->name : nulldevname;
	for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned int); i++) {
		ret |= (((const unsigned int *)indev)[i]
			^ ((const unsigned int *)info->physindev)[i])
			& ((const unsigned int *)info->in_mask)[i];
	}

87
	if ((ret == 0) ^ !(info->invert & XT_PHYSDEV_OP_IN))
L
Linus Torvalds 已提交
88 89 90
		return NOMATCH;

match_outdev:
91
	if (!(info->bitmask & XT_PHYSDEV_OP_OUT))
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100
		return MATCH;
	outdev = nf_bridge->physoutdev ?
		 nf_bridge->physoutdev->name : nulldevname;
	for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned int); i++) {
		ret |= (((const unsigned int *)outdev)[i]
			^ ((const unsigned int *)info->physoutdev)[i])
			& ((const unsigned int *)info->out_mask)[i];
	}

101
	return (ret != 0) ^ !(info->invert & XT_PHYSDEV_OP_OUT);
L
Linus Torvalds 已提交
102 103 104 105
}

static int
checkentry(const char *tablename,
106
		       const void *ip,
107
		       const struct xt_match *match,
L
Linus Torvalds 已提交
108 109 110 111
		       void *matchinfo,
		       unsigned int matchsize,
		       unsigned int hook_mask)
{
112
	const struct xt_physdev_info *info = matchinfo;
L
Linus Torvalds 已提交
113

114 115
	if (!(info->bitmask & XT_PHYSDEV_OP_MASK) ||
	    info->bitmask & ~XT_PHYSDEV_OP_MASK)
L
Linus Torvalds 已提交
116
		return 0;
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	if (brnf_deferred_hooks == 0 &&
	    info->bitmask & XT_PHYSDEV_OP_OUT &&
	    (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) ||
	     info->invert & XT_PHYSDEV_OP_BRIDGED) &&
	    hook_mask & ((1 << NF_IP_LOCAL_OUT) | (1 << NF_IP_FORWARD) |
	                 (1 << NF_IP_POST_ROUTING))) {
		printk(KERN_WARNING "physdev match: using --physdev-out in the "
		       "OUTPUT, FORWARD and POSTROUTING chains for non-bridged "
		       "traffic is deprecated and breaks other things, it will "
		       "be removed in January 2007. See Documentation/"
		       "feature-removal-schedule.txt for details. This doesn't "
		       "affect you in case you're using it for purely bridged "
		       "traffic.\n");
		brnf_deferred_hooks = 1;
	}
L
Linus Torvalds 已提交
132 133 134
	return 1;
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
static struct xt_match xt_physdev_match[] = {
	{
		.name		= "physdev",
		.family		= AF_INET,
		.checkentry	= checkentry,
		.match		= match,
		.matchsize	= sizeof(struct xt_physdev_info),
		.me		= THIS_MODULE,
	},
	{
		.name		= "physdev",
		.family		= AF_INET6,
		.checkentry	= checkentry,
		.match		= match,
		.matchsize	= sizeof(struct xt_physdev_info),
		.me		= THIS_MODULE,
	},
L
Linus Torvalds 已提交
152 153
};

154
static int __init xt_physdev_init(void)
L
Linus Torvalds 已提交
155
{
156 157
	return xt_register_matches(xt_physdev_match,
				   ARRAY_SIZE(xt_physdev_match));
L
Linus Torvalds 已提交
158 159
}

160
static void __exit xt_physdev_fini(void)
L
Linus Torvalds 已提交
161
{
162
	xt_unregister_matches(xt_physdev_match, ARRAY_SIZE(xt_physdev_match));
L
Linus Torvalds 已提交
163 164
}

165 166
module_init(xt_physdev_init);
module_exit(xt_physdev_fini);