1
2
3
4
5
6
7
8
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11#include <linux/module.h>
12#include <linux/skbuff.h>
13#include <linux/netfilter_bridge.h>
14#include <linux/netfilter/xt_physdev.h>
15#include <linux/netfilter/x_tables.h>
16#include <net/netfilter/br_netfilter.h>
17
18MODULE_LICENSE("GPL");
19MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
20MODULE_DESCRIPTION("Xtables: Bridge physical device match");
21MODULE_ALIAS("ipt_physdev");
22MODULE_ALIAS("ip6t_physdev");
23
24
25static bool
26physdev_mt(const struct sk_buff *skb, struct xt_action_param *par)
27{
28 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
29 const struct xt_physdev_info *info = par->matchinfo;
30 unsigned long ret;
31 const char *indev, *outdev;
32 const struct nf_bridge_info *nf_bridge;
33
34
35
36
37 if (!(nf_bridge = skb->nf_bridge)) {
38
39 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
40 !(info->invert & XT_PHYSDEV_OP_BRIDGED))
41 return false;
42 if ((info->bitmask & XT_PHYSDEV_OP_ISIN) &&
43 !(info->invert & XT_PHYSDEV_OP_ISIN))
44 return false;
45 if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) &&
46 !(info->invert & XT_PHYSDEV_OP_ISOUT))
47 return false;
48 if ((info->bitmask & XT_PHYSDEV_OP_IN) &&
49 !(info->invert & XT_PHYSDEV_OP_IN))
50 return false;
51 if ((info->bitmask & XT_PHYSDEV_OP_OUT) &&
52 !(info->invert & XT_PHYSDEV_OP_OUT))
53 return false;
54 return true;
55 }
56
57
58 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
59 (!!nf_bridge->physoutdev ^ !(info->invert & XT_PHYSDEV_OP_BRIDGED)))
60 return false;
61
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))))
66 return false;
67
68 if (!(info->bitmask & XT_PHYSDEV_OP_IN))
69 goto match_outdev;
70 indev = nf_bridge->physindev ? nf_bridge->physindev->name : nulldevname;
71 ret = ifname_compare_aligned(indev, info->physindev, info->in_mask);
72
73 if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN))
74 return false;
75
76match_outdev:
77 if (!(info->bitmask & XT_PHYSDEV_OP_OUT))
78 return true;
79 outdev = nf_bridge->physoutdev ?
80 nf_bridge->physoutdev->name : nulldevname;
81 ret = ifname_compare_aligned(outdev, info->physoutdev, info->out_mask);
82
83 return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT));
84}
85
86static int physdev_mt_check(const struct xt_mtchk_param *par)
87{
88 const struct xt_physdev_info *info = par->matchinfo;
89
90 br_netfilter_enable();
91
92 if (!(info->bitmask & XT_PHYSDEV_OP_MASK) ||
93 info->bitmask & ~XT_PHYSDEV_OP_MASK)
94 return -EINVAL;
95 if (info->bitmask & (XT_PHYSDEV_OP_OUT | XT_PHYSDEV_OP_ISOUT) &&
96 (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) ||
97 info->invert & XT_PHYSDEV_OP_BRIDGED) &&
98 par->hook_mask & ((1 << NF_INET_LOCAL_OUT) |
99 (1 << NF_INET_FORWARD) | (1 << NF_INET_POST_ROUTING))) {
100 pr_info("using --physdev-out and --physdev-is-out are only "
101 "supported in the FORWARD and POSTROUTING chains with "
102 "bridged traffic.\n");
103 if (par->hook_mask & (1 << NF_INET_LOCAL_OUT))
104 return -EINVAL;
105 }
106 return 0;
107}
108
109static struct xt_match physdev_mt_reg __read_mostly = {
110 .name = "physdev",
111 .revision = 0,
112 .family = NFPROTO_UNSPEC,
113 .checkentry = physdev_mt_check,
114 .match = physdev_mt,
115 .matchsize = sizeof(struct xt_physdev_info),
116 .me = THIS_MODULE,
117};
118
119static int __init physdev_mt_init(void)
120{
121 return xt_register_match(&physdev_mt_reg);
122}
123
124static void __exit physdev_mt_exit(void)
125{
126 xt_unregister_match(&physdev_mt_reg);
127}
128
129module_init(physdev_mt_init);
130module_exit(physdev_mt_exit);
131