Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* 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> | |
deb47c66 | 13 | #include <linux/netfilter_bridge.h> |
2e4e6a17 HW |
14 | #include <linux/netfilter/xt_physdev.h> |
15 | #include <linux/netfilter/x_tables.h> | |
1da177e4 LT |
16 | |
17 | MODULE_LICENSE("GPL"); | |
18 | MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>"); | |
2ae15b64 | 19 | MODULE_DESCRIPTION("Xtables: Bridge physical device match"); |
2e4e6a17 HW |
20 | MODULE_ALIAS("ipt_physdev"); |
21 | MODULE_ALIAS("ip6t_physdev"); | |
1da177e4 | 22 | |
eacc17fb | 23 | |
1d93a9cb | 24 | static bool |
f7108a20 | 25 | physdev_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
1da177e4 | 26 | { |
4f1c3b7e | 27 | static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long)))); |
f7108a20 | 28 | const struct xt_physdev_info *info = par->matchinfo; |
4f1c3b7e | 29 | unsigned long ret; |
1da177e4 | 30 | const char *indev, *outdev; |
a47362a2 | 31 | const struct nf_bridge_info *nf_bridge; |
1da177e4 LT |
32 | |
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 */ | |
2e4e6a17 HW |
38 | if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) && |
39 | !(info->invert & XT_PHYSDEV_OP_BRIDGED)) | |
1d93a9cb | 40 | return false; |
2e4e6a17 HW |
41 | if ((info->bitmask & XT_PHYSDEV_OP_ISIN) && |
42 | !(info->invert & XT_PHYSDEV_OP_ISIN)) | |
1d93a9cb | 43 | return false; |
2e4e6a17 HW |
44 | if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) && |
45 | !(info->invert & XT_PHYSDEV_OP_ISOUT)) | |
1d93a9cb | 46 | return false; |
2e4e6a17 HW |
47 | if ((info->bitmask & XT_PHYSDEV_OP_IN) && |
48 | !(info->invert & XT_PHYSDEV_OP_IN)) | |
1d93a9cb | 49 | return false; |
2e4e6a17 HW |
50 | if ((info->bitmask & XT_PHYSDEV_OP_OUT) && |
51 | !(info->invert & XT_PHYSDEV_OP_OUT)) | |
1d93a9cb JE |
52 | return false; |
53 | return true; | |
1da177e4 LT |
54 | } |
55 | ||
56 | /* This only makes sense in the FORWARD and POSTROUTING chains */ | |
2e4e6a17 | 57 | if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) && |
1da177e4 | 58 | (!!(nf_bridge->mask & BRNF_BRIDGED) ^ |
2e4e6a17 | 59 | !(info->invert & XT_PHYSDEV_OP_BRIDGED))) |
1d93a9cb | 60 | return false; |
1da177e4 | 61 | |
2e4e6a17 HW |
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)))) | |
1d93a9cb | 66 | return false; |
1da177e4 | 67 | |
2e4e6a17 | 68 | if (!(info->bitmask & XT_PHYSDEV_OP_IN)) |
1da177e4 LT |
69 | goto match_outdev; |
70 | indev = nf_bridge->physindev ? nf_bridge->physindev->name : nulldevname; | |
b8dfe498 | 71 | ret = ifname_compare_aligned(indev, info->physindev, info->in_mask); |
1da177e4 | 72 | |
1d93a9cb JE |
73 | if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN)) |
74 | return false; | |
1da177e4 LT |
75 | |
76 | match_outdev: | |
2e4e6a17 | 77 | if (!(info->bitmask & XT_PHYSDEV_OP_OUT)) |
1d93a9cb | 78 | return true; |
1da177e4 LT |
79 | outdev = nf_bridge->physoutdev ? |
80 | nf_bridge->physoutdev->name : nulldevname; | |
b8dfe498 | 81 | ret = ifname_compare_aligned(outdev, info->physoutdev, info->out_mask); |
eacc17fb | 82 | |
4f1c3b7e | 83 | return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT)); |
1da177e4 LT |
84 | } |
85 | ||
9b4fce7a | 86 | static bool physdev_mt_check(const struct xt_mtchk_param *par) |
1da177e4 | 87 | { |
9b4fce7a | 88 | const struct xt_physdev_info *info = par->matchinfo; |
1da177e4 | 89 | |
2e4e6a17 HW |
90 | if (!(info->bitmask & XT_PHYSDEV_OP_MASK) || |
91 | info->bitmask & ~XT_PHYSDEV_OP_MASK) | |
ccb79bdc | 92 | return false; |
2bf540b7 | 93 | if (info->bitmask & XT_PHYSDEV_OP_OUT && |
10ea6ac8 PM |
94 | (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) || |
95 | info->invert & XT_PHYSDEV_OP_BRIDGED) && | |
9b4fce7a JE |
96 | par->hook_mask & ((1 << NF_INET_LOCAL_OUT) | |
97 | (1 << NF_INET_FORWARD) | (1 << NF_INET_POST_ROUTING))) { | |
10ea6ac8 PM |
98 | printk(KERN_WARNING "physdev match: using --physdev-out in the " |
99 | "OUTPUT, FORWARD and POSTROUTING chains for non-bridged " | |
2bf540b7 | 100 | "traffic is not supported anymore.\n"); |
9b4fce7a | 101 | if (par->hook_mask & (1 << NF_INET_LOCAL_OUT)) |
ccb79bdc | 102 | return false; |
10ea6ac8 | 103 | } |
ccb79bdc | 104 | return true; |
1da177e4 LT |
105 | } |
106 | ||
ab4f21e6 JE |
107 | static struct xt_match physdev_mt_reg __read_mostly = { |
108 | .name = "physdev", | |
109 | .revision = 0, | |
110 | .family = NFPROTO_UNSPEC, | |
111 | .checkentry = physdev_mt_check, | |
112 | .match = physdev_mt, | |
113 | .matchsize = sizeof(struct xt_physdev_info), | |
114 | .me = THIS_MODULE, | |
1da177e4 LT |
115 | }; |
116 | ||
d3c5ee6d | 117 | static int __init physdev_mt_init(void) |
1da177e4 | 118 | { |
ab4f21e6 | 119 | return xt_register_match(&physdev_mt_reg); |
1da177e4 LT |
120 | } |
121 | ||
d3c5ee6d | 122 | static void __exit physdev_mt_exit(void) |
1da177e4 | 123 | { |
ab4f21e6 | 124 | xt_unregister_match(&physdev_mt_reg); |
1da177e4 LT |
125 | } |
126 | ||
d3c5ee6d JE |
127 | module_init(physdev_mt_init); |
128 | module_exit(physdev_mt_exit); |