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>
16 #include <linux/netfilter_bridge.h>
18 MODULE_LICENSE("GPL");
19 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
20 MODULE_DESCRIPTION("iptables bridge physical device match module");
21 MODULE_ALIAS("ipt_physdev");
22 MODULE_ALIAS("ip6t_physdev");
25 match(const struct sk_buff
*skb
,
26 const struct net_device
*in
,
27 const struct net_device
*out
,
28 const struct xt_match
*match
,
29 const void *matchinfo
,
35 static const char nulldevname
[IFNAMSIZ
];
36 const struct xt_physdev_info
*info
= matchinfo
;
38 const char *indev
, *outdev
;
39 const struct nf_bridge_info
*nf_bridge
;
41 /* Not a bridged IP packet or no info available yet:
42 * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if
43 * the destination device will be a bridge. */
44 if (!(nf_bridge
= skb
->nf_bridge
)) {
45 /* Return MATCH if the invert flags of the used options are on */
46 if ((info
->bitmask
& XT_PHYSDEV_OP_BRIDGED
) &&
47 !(info
->invert
& XT_PHYSDEV_OP_BRIDGED
))
49 if ((info
->bitmask
& XT_PHYSDEV_OP_ISIN
) &&
50 !(info
->invert
& XT_PHYSDEV_OP_ISIN
))
52 if ((info
->bitmask
& XT_PHYSDEV_OP_ISOUT
) &&
53 !(info
->invert
& XT_PHYSDEV_OP_ISOUT
))
55 if ((info
->bitmask
& XT_PHYSDEV_OP_IN
) &&
56 !(info
->invert
& XT_PHYSDEV_OP_IN
))
58 if ((info
->bitmask
& XT_PHYSDEV_OP_OUT
) &&
59 !(info
->invert
& XT_PHYSDEV_OP_OUT
))
64 /* This only makes sense in the FORWARD and POSTROUTING chains */
65 if ((info
->bitmask
& XT_PHYSDEV_OP_BRIDGED
) &&
66 (!!(nf_bridge
->mask
& BRNF_BRIDGED
) ^
67 !(info
->invert
& XT_PHYSDEV_OP_BRIDGED
)))
70 if ((info
->bitmask
& XT_PHYSDEV_OP_ISIN
&&
71 (!nf_bridge
->physindev
^ !!(info
->invert
& XT_PHYSDEV_OP_ISIN
))) ||
72 (info
->bitmask
& XT_PHYSDEV_OP_ISOUT
&&
73 (!nf_bridge
->physoutdev
^ !!(info
->invert
& XT_PHYSDEV_OP_ISOUT
))))
76 if (!(info
->bitmask
& XT_PHYSDEV_OP_IN
))
78 indev
= nf_bridge
->physindev
? nf_bridge
->physindev
->name
: nulldevname
;
79 for (i
= 0, ret
= false; i
< IFNAMSIZ
/sizeof(unsigned int); i
++) {
80 ret
|= (((const unsigned int *)indev
)[i
]
81 ^ ((const unsigned int *)info
->physindev
)[i
])
82 & ((const unsigned int *)info
->in_mask
)[i
];
85 if (!ret
^ !(info
->invert
& XT_PHYSDEV_OP_IN
))
89 if (!(info
->bitmask
& XT_PHYSDEV_OP_OUT
))
91 outdev
= nf_bridge
->physoutdev
?
92 nf_bridge
->physoutdev
->name
: nulldevname
;
93 for (i
= 0, ret
= false; i
< IFNAMSIZ
/sizeof(unsigned int); i
++) {
94 ret
|= (((const unsigned int *)outdev
)[i
]
95 ^ ((const unsigned int *)info
->physoutdev
)[i
])
96 & ((const unsigned int *)info
->out_mask
)[i
];
99 return ret
^ !(info
->invert
& XT_PHYSDEV_OP_OUT
);
103 checkentry(const char *tablename
,
105 const struct xt_match
*match
,
107 unsigned int hook_mask
)
109 const struct xt_physdev_info
*info
= matchinfo
;
111 if (!(info
->bitmask
& XT_PHYSDEV_OP_MASK
) ||
112 info
->bitmask
& ~XT_PHYSDEV_OP_MASK
)
114 if (info
->bitmask
& XT_PHYSDEV_OP_OUT
&&
115 (!(info
->bitmask
& XT_PHYSDEV_OP_BRIDGED
) ||
116 info
->invert
& XT_PHYSDEV_OP_BRIDGED
) &&
117 hook_mask
& ((1 << NF_IP_LOCAL_OUT
) | (1 << NF_IP_FORWARD
) |
118 (1 << NF_IP_POST_ROUTING
))) {
119 printk(KERN_WARNING
"physdev match: using --physdev-out in the "
120 "OUTPUT, FORWARD and POSTROUTING chains for non-bridged "
121 "traffic is not supported anymore.\n");
122 if (hook_mask
& (1 << NF_IP_LOCAL_OUT
))
128 static struct xt_match xt_physdev_match
[] __read_mostly
= {
132 .checkentry
= checkentry
,
134 .matchsize
= sizeof(struct xt_physdev_info
),
140 .checkentry
= checkentry
,
142 .matchsize
= sizeof(struct xt_physdev_info
),
147 static int __init
xt_physdev_init(void)
149 return xt_register_matches(xt_physdev_match
,
150 ARRAY_SIZE(xt_physdev_match
));
153 static void __exit
xt_physdev_fini(void)
155 xt_unregister_matches(xt_physdev_match
, ARRAY_SIZE(xt_physdev_match
));
158 module_init(xt_physdev_init
);
159 module_exit(xt_physdev_fini
);