blob: 4b13ef7ce14575534e6012a31d8964f4e51e7d5d [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 Engelhardtf7108a22008-10-08 11:35:18 +020024physdev_mt(const struct sk_buff *skb, const struct xt_match_param *par)
Harald Welte2e4e6a12006-01-12 13:30:04 -080025{
26 int i;
Eric Dumazet4f1c3b72009-02-18 19:11:39 +010027 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Jan Engelhardtf7108a22008-10-08 11:35:18 +020028 const struct xt_physdev_info *info = par->matchinfo;
Eric Dumazet4f1c3b72009-02-18 19:11:39 +010029 unsigned long ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -080030 const char *indev, *outdev;
Jan Engelhardta47362a2007-07-07 22:16:55 -070031 const struct nf_bridge_info *nf_bridge;
Harald Welte2e4e6a12006-01-12 13:30:04 -080032
33 /* Not a bridged IP packet or no info available yet:
34 * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if
35 * the destination device will be a bridge. */
36 if (!(nf_bridge = skb->nf_bridge)) {
37 /* Return MATCH if the invert flags of the used options are on */
38 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
39 !(info->invert & XT_PHYSDEV_OP_BRIDGED))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070040 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080041 if ((info->bitmask & XT_PHYSDEV_OP_ISIN) &&
42 !(info->invert & XT_PHYSDEV_OP_ISIN))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070043 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080044 if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) &&
45 !(info->invert & XT_PHYSDEV_OP_ISOUT))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070046 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080047 if ((info->bitmask & XT_PHYSDEV_OP_IN) &&
48 !(info->invert & XT_PHYSDEV_OP_IN))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070049 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080050 if ((info->bitmask & XT_PHYSDEV_OP_OUT) &&
51 !(info->invert & XT_PHYSDEV_OP_OUT))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070052 return false;
53 return true;
Harald Welte2e4e6a12006-01-12 13:30:04 -080054 }
55
56 /* This only makes sense in the FORWARD and POSTROUTING chains */
57 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
58 (!!(nf_bridge->mask & BRNF_BRIDGED) ^
59 !(info->invert & XT_PHYSDEV_OP_BRIDGED)))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070060 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080061
62 if ((info->bitmask & XT_PHYSDEV_OP_ISIN &&
63 (!nf_bridge->physindev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) ||
64 (info->bitmask & XT_PHYSDEV_OP_ISOUT &&
65 (!nf_bridge->physoutdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT))))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070066 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080067
68 if (!(info->bitmask & XT_PHYSDEV_OP_IN))
69 goto match_outdev;
70 indev = nf_bridge->physindev ? nf_bridge->physindev->name : nulldevname;
Eric Dumazet4f1c3b72009-02-18 19:11:39 +010071 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
72 ret |= (((const unsigned long *)indev)[i]
73 ^ ((const unsigned long *)info->physindev)[i])
74 & ((const unsigned long *)info->in_mask)[i];
Harald Welte2e4e6a12006-01-12 13:30:04 -080075 }
76
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070077 if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN))
78 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080079
80match_outdev:
81 if (!(info->bitmask & XT_PHYSDEV_OP_OUT))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070082 return true;
Harald Welte2e4e6a12006-01-12 13:30:04 -080083 outdev = nf_bridge->physoutdev ?
84 nf_bridge->physoutdev->name : nulldevname;
Eric Dumazet4f1c3b72009-02-18 19:11:39 +010085 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
86 ret |= (((const unsigned long *)outdev)[i]
87 ^ ((const unsigned long *)info->physoutdev)[i])
88 & ((const unsigned long *)info->out_mask)[i];
Harald Welte2e4e6a12006-01-12 13:30:04 -080089 }
Eric Dumazet4f1c3b72009-02-18 19:11:39 +010090 return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT));
Harald Welte2e4e6a12006-01-12 13:30:04 -080091}
92
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020093static bool physdev_mt_check(const struct xt_mtchk_param *par)
Harald Welte2e4e6a12006-01-12 13:30:04 -080094{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020095 const struct xt_physdev_info *info = par->matchinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -080096
Harald Welte2e4e6a12006-01-12 13:30:04 -080097 if (!(info->bitmask & XT_PHYSDEV_OP_MASK) ||
98 info->bitmask & ~XT_PHYSDEV_OP_MASK)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070099 return false;
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800100 if (info->bitmask & XT_PHYSDEV_OP_OUT &&
Patrick McHardy10ea6ac2006-07-24 22:54:55 -0700101 (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) ||
102 info->invert & XT_PHYSDEV_OP_BRIDGED) &&
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200103 par->hook_mask & ((1 << NF_INET_LOCAL_OUT) |
104 (1 << NF_INET_FORWARD) | (1 << NF_INET_POST_ROUTING))) {
Patrick McHardy10ea6ac2006-07-24 22:54:55 -0700105 printk(KERN_WARNING "physdev match: using --physdev-out in the "
106 "OUTPUT, FORWARD and POSTROUTING chains for non-bridged "
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800107 "traffic is not supported anymore.\n");
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200108 if (par->hook_mask & (1 << NF_INET_LOCAL_OUT))
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700109 return false;
Patrick McHardy10ea6ac2006-07-24 22:54:55 -0700110 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700111 return true;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800112}
113
Jan Engelhardtab4f21e2008-10-08 11:35:20 +0200114static struct xt_match physdev_mt_reg __read_mostly = {
115 .name = "physdev",
116 .revision = 0,
117 .family = NFPROTO_UNSPEC,
118 .checkentry = physdev_mt_check,
119 .match = physdev_mt,
120 .matchsize = sizeof(struct xt_physdev_info),
121 .me = THIS_MODULE,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800122};
123
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800124static int __init physdev_mt_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800125{
Jan Engelhardtab4f21e2008-10-08 11:35:20 +0200126 return xt_register_match(&physdev_mt_reg);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800127}
128
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800129static void __exit physdev_mt_exit(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800130{
Jan Engelhardtab4f21e2008-10-08 11:35:20 +0200131 xt_unregister_match(&physdev_mt_reg);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800132}
133
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800134module_init(physdev_mt_init);
135module_exit(physdev_mt_exit);