blob: 4ec1094bda929771fbe13e403f3fcb98e0fa2b8e [file] [log] [blame]
Harald Welte2e4e6a12006-01-12 13:30:04 -08001/* Kernel module to match the bridge port in and
2 * out device for IP packets coming into contact with a bridge. */
3
4/* (C) 2001-2003 Bart De Schuymer <bdschuym@pandora.be>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12#include <linux/skbuff.h>
Andrew Mortondeb47c62006-08-15 00:04:56 -070013#include <linux/netfilter_bridge.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080014#include <linux/netfilter/xt_physdev.h>
15#include <linux/netfilter/x_tables.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080016
17MODULE_LICENSE("GPL");
18MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080019MODULE_DESCRIPTION("Xtables: Bridge physical device match");
Harald Welte2e4e6a12006-01-12 13:30:04 -080020MODULE_ALIAS("ipt_physdev");
21MODULE_ALIAS("ip6t_physdev");
22
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070023static bool
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080024physdev_mt(const struct sk_buff *skb, const struct net_device *in,
25 const struct net_device *out, const struct xt_match *match,
26 const void *matchinfo, int offset, unsigned int protoff,
27 bool *hotdrop)
Harald Welte2e4e6a12006-01-12 13:30:04 -080028{
29 int i;
30 static const char nulldevname[IFNAMSIZ];
31 const struct xt_physdev_info *info = matchinfo;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070032 bool ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -080033 const char *indev, *outdev;
Jan Engelhardta47362a2007-07-07 22:16:55 -070034 const struct nf_bridge_info *nf_bridge;
Harald Welte2e4e6a12006-01-12 13:30:04 -080035
36 /* Not a bridged IP packet or no info available yet:
37 * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if
38 * the destination device will be a bridge. */
39 if (!(nf_bridge = skb->nf_bridge)) {
40 /* Return MATCH if the invert flags of the used options are on */
41 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
42 !(info->invert & XT_PHYSDEV_OP_BRIDGED))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070043 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080044 if ((info->bitmask & XT_PHYSDEV_OP_ISIN) &&
45 !(info->invert & XT_PHYSDEV_OP_ISIN))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070046 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080047 if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) &&
48 !(info->invert & XT_PHYSDEV_OP_ISOUT))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070049 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080050 if ((info->bitmask & XT_PHYSDEV_OP_IN) &&
51 !(info->invert & XT_PHYSDEV_OP_IN))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070052 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080053 if ((info->bitmask & XT_PHYSDEV_OP_OUT) &&
54 !(info->invert & XT_PHYSDEV_OP_OUT))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070055 return false;
56 return true;
Harald Welte2e4e6a12006-01-12 13:30:04 -080057 }
58
59 /* This only makes sense in the FORWARD and POSTROUTING chains */
60 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
61 (!!(nf_bridge->mask & BRNF_BRIDGED) ^
62 !(info->invert & XT_PHYSDEV_OP_BRIDGED)))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070063 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080064
65 if ((info->bitmask & XT_PHYSDEV_OP_ISIN &&
66 (!nf_bridge->physindev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) ||
67 (info->bitmask & XT_PHYSDEV_OP_ISOUT &&
68 (!nf_bridge->physoutdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT))))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070069 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080070
71 if (!(info->bitmask & XT_PHYSDEV_OP_IN))
72 goto match_outdev;
73 indev = nf_bridge->physindev ? nf_bridge->physindev->name : nulldevname;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070074 for (i = 0, ret = false; i < IFNAMSIZ/sizeof(unsigned int); i++) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080075 ret |= (((const unsigned int *)indev)[i]
76 ^ ((const unsigned int *)info->physindev)[i])
77 & ((const unsigned int *)info->in_mask)[i];
78 }
79
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070080 if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN))
81 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080082
83match_outdev:
84 if (!(info->bitmask & XT_PHYSDEV_OP_OUT))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070085 return true;
Harald Welte2e4e6a12006-01-12 13:30:04 -080086 outdev = nf_bridge->physoutdev ?
87 nf_bridge->physoutdev->name : nulldevname;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070088 for (i = 0, ret = false; i < IFNAMSIZ/sizeof(unsigned int); i++) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080089 ret |= (((const unsigned int *)outdev)[i]
90 ^ ((const unsigned int *)info->physoutdev)[i])
91 & ((const unsigned int *)info->out_mask)[i];
92 }
93
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070094 return ret ^ !(info->invert & XT_PHYSDEV_OP_OUT);
Harald Welte2e4e6a12006-01-12 13:30:04 -080095}
96
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070097static bool
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080098physdev_mt_check(const char *tablename, const void *ip,
99 const struct xt_match *match, void *matchinfo,
100 unsigned int hook_mask)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800101{
102 const struct xt_physdev_info *info = matchinfo;
103
Harald Welte2e4e6a12006-01-12 13:30:04 -0800104 if (!(info->bitmask & XT_PHYSDEV_OP_MASK) ||
105 info->bitmask & ~XT_PHYSDEV_OP_MASK)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700106 return false;
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800107 if (info->bitmask & XT_PHYSDEV_OP_OUT &&
Patrick McHardy10ea6ac2006-07-24 22:54:55 -0700108 (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) ||
109 info->invert & XT_PHYSDEV_OP_BRIDGED) &&
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800110 hook_mask & ((1 << NF_INET_LOCAL_OUT) | (1 << NF_INET_FORWARD) |
111 (1 << NF_INET_POST_ROUTING))) {
Patrick McHardy10ea6ac2006-07-24 22:54:55 -0700112 printk(KERN_WARNING "physdev match: using --physdev-out in the "
113 "OUTPUT, FORWARD and POSTROUTING chains for non-bridged "
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800114 "traffic is not supported anymore.\n");
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800115 if (hook_mask & (1 << NF_INET_LOCAL_OUT))
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700116 return false;
Patrick McHardy10ea6ac2006-07-24 22:54:55 -0700117 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700118 return true;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800119}
120
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800121static struct xt_match physdev_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700122 {
123 .name = "physdev",
124 .family = AF_INET,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800125 .checkentry = physdev_mt_check,
126 .match = physdev_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700127 .matchsize = sizeof(struct xt_physdev_info),
128 .me = THIS_MODULE,
129 },
130 {
131 .name = "physdev",
132 .family = AF_INET6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800133 .checkentry = physdev_mt_check,
134 .match = physdev_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700135 .matchsize = sizeof(struct xt_physdev_info),
136 .me = THIS_MODULE,
137 },
Harald Welte2e4e6a12006-01-12 13:30:04 -0800138};
139
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800140static int __init physdev_mt_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800141{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800142 return xt_register_matches(physdev_mt_reg, ARRAY_SIZE(physdev_mt_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800143}
144
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800145static void __exit physdev_mt_exit(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800146{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800147 xt_unregister_matches(physdev_mt_reg, ARRAY_SIZE(physdev_mt_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800148}
149
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800150module_init(physdev_mt_init);
151module_exit(physdev_mt_exit);