1 /* Kernel module to match the bridge port in and
2 * out device for IP packets coming into contact with a bridge. */
4 /* (C) 2001-2003 Bart De Schuymer <bdschuym@pandora.be>
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.
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>
17 MODULE_LICENSE("GPL");
18 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
19 MODULE_DESCRIPTION("Xtables: Bridge physical device match");
20 MODULE_ALIAS("ipt_physdev");
21 MODULE_ALIAS("ip6t_physdev");
24 physdev_mt(const struct sk_buff *skb, const struct xt_match_param *par)
27 static const char nulldevname[IFNAMSIZ];
28 const struct xt_physdev_info *info = par->matchinfo;
30 const char *indev, *outdev;
31 const struct nf_bridge_info *nf_bridge;
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))
41 if ((info->bitmask & XT_PHYSDEV_OP_ISIN) &&
42 !(info->invert & XT_PHYSDEV_OP_ISIN))
44 if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) &&
45 !(info->invert & XT_PHYSDEV_OP_ISOUT))
47 if ((info->bitmask & XT_PHYSDEV_OP_IN) &&
48 !(info->invert & XT_PHYSDEV_OP_IN))
50 if ((info->bitmask & XT_PHYSDEV_OP_OUT) &&
51 !(info->invert & XT_PHYSDEV_OP_OUT))
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)))
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))))
68 if (!(info->bitmask & XT_PHYSDEV_OP_IN))
70 indev = nf_bridge->physindev ? nf_bridge->physindev->name : nulldevname;
71 for (i = 0, ret = false; i < IFNAMSIZ/sizeof(unsigned int); i++) {
72 ret |= (((const unsigned int *)indev)[i]
73 ^ ((const unsigned int *)info->physindev)[i])
74 & ((const unsigned int *)info->in_mask)[i];
77 if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN))
81 if (!(info->bitmask & XT_PHYSDEV_OP_OUT))
83 outdev = nf_bridge->physoutdev ?
84 nf_bridge->physoutdev->name : nulldevname;
85 for (i = 0, ret = false; i < IFNAMSIZ/sizeof(unsigned int); i++) {
86 ret |= (((const unsigned int *)outdev)[i]
87 ^ ((const unsigned int *)info->physoutdev)[i])
88 & ((const unsigned int *)info->out_mask)[i];
91 return ret ^ !(info->invert & XT_PHYSDEV_OP_OUT);
94 static bool physdev_mt_check(const struct xt_mtchk_param *par)
96 const struct xt_physdev_info *info = par->matchinfo;
98 if (!(info->bitmask & XT_PHYSDEV_OP_MASK) ||
99 info->bitmask & ~XT_PHYSDEV_OP_MASK)
101 if (info->bitmask & XT_PHYSDEV_OP_OUT &&
102 (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) ||
103 info->invert & XT_PHYSDEV_OP_BRIDGED) &&
104 par->hook_mask & ((1 << NF_INET_LOCAL_OUT) |
105 (1 << NF_INET_FORWARD) | (1 << NF_INET_POST_ROUTING))) {
106 printk(KERN_WARNING "physdev match: using --physdev-out in the "
107 "OUTPUT, FORWARD and POSTROUTING chains for non-bridged "
108 "traffic is not supported anymore.\n");
109 if (par->hook_mask & (1 << NF_INET_LOCAL_OUT))
115 static struct xt_match physdev_mt_reg __read_mostly = {
118 .family = NFPROTO_UNSPEC,
119 .checkentry = physdev_mt_check,
121 .matchsize = sizeof(struct xt_physdev_info),
125 static int __init physdev_mt_init(void)
127 return xt_register_match(&physdev_mt_reg);
130 static void __exit physdev_mt_exit(void)
132 xt_unregister_match(&physdev_mt_reg);
135 module_init(physdev_mt_init);
136 module_exit(physdev_mt_exit);