]>
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 <[email protected]> | |
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 | */ | |
8bee4bad | 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
1da177e4 LT |
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> | |
4b7fd5d9 | 16 | #include <net/netfilter/br_netfilter.h> |
1da177e4 LT |
17 | |
18 | MODULE_LICENSE("GPL"); | |
19 | MODULE_AUTHOR("Bart De Schuymer <[email protected]>"); | |
2ae15b64 | 20 | MODULE_DESCRIPTION("Xtables: Bridge physical device match"); |
2e4e6a17 HW |
21 | MODULE_ALIAS("ipt_physdev"); |
22 | MODULE_ALIAS("ip6t_physdev"); | |
1da177e4 | 23 | |
eacc17fb | 24 | |
1d93a9cb | 25 | static bool |
62fc8051 | 26 | physdev_mt(const struct sk_buff *skb, struct xt_action_param *par) |
1da177e4 | 27 | { |
4f1c3b7e | 28 | static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long)))); |
f7108a20 | 29 | const struct xt_physdev_info *info = par->matchinfo; |
4f1c3b7e | 30 | unsigned long ret; |
1da177e4 | 31 | const char *indev, *outdev; |
a47362a2 | 32 | const struct nf_bridge_info *nf_bridge; |
1da177e4 LT |
33 | |
34 | /* Not a bridged IP packet or no info available yet: | |
35 | * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if | |
36 | * the destination device will be a bridge. */ | |
37 | if (!(nf_bridge = skb->nf_bridge)) { | |
38 | /* Return MATCH if the invert flags of the used options are on */ | |
2e4e6a17 HW |
39 | if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) && |
40 | !(info->invert & XT_PHYSDEV_OP_BRIDGED)) | |
1d93a9cb | 41 | return false; |
2e4e6a17 HW |
42 | if ((info->bitmask & XT_PHYSDEV_OP_ISIN) && |
43 | !(info->invert & XT_PHYSDEV_OP_ISIN)) | |
1d93a9cb | 44 | return false; |
2e4e6a17 HW |
45 | if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) && |
46 | !(info->invert & XT_PHYSDEV_OP_ISOUT)) | |
1d93a9cb | 47 | return false; |
2e4e6a17 HW |
48 | if ((info->bitmask & XT_PHYSDEV_OP_IN) && |
49 | !(info->invert & XT_PHYSDEV_OP_IN)) | |
1d93a9cb | 50 | return false; |
2e4e6a17 HW |
51 | if ((info->bitmask & XT_PHYSDEV_OP_OUT) && |
52 | !(info->invert & XT_PHYSDEV_OP_OUT)) | |
1d93a9cb JE |
53 | return false; |
54 | return true; | |
1da177e4 LT |
55 | } |
56 | ||
57 | /* This only makes sense in the FORWARD and POSTROUTING chains */ | |
2e4e6a17 | 58 | if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) && |
1da177e4 | 59 | (!!(nf_bridge->mask & BRNF_BRIDGED) ^ |
2e4e6a17 | 60 | !(info->invert & XT_PHYSDEV_OP_BRIDGED))) |
1d93a9cb | 61 | return false; |
1da177e4 | 62 | |
2e4e6a17 HW |
63 | if ((info->bitmask & XT_PHYSDEV_OP_ISIN && |
64 | (!nf_bridge->physindev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) || | |
65 | (info->bitmask & XT_PHYSDEV_OP_ISOUT && | |
66 | (!nf_bridge->physoutdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT)))) | |
1d93a9cb | 67 | return false; |
1da177e4 | 68 | |
2e4e6a17 | 69 | if (!(info->bitmask & XT_PHYSDEV_OP_IN)) |
1da177e4 LT |
70 | goto match_outdev; |
71 | indev = nf_bridge->physindev ? nf_bridge->physindev->name : nulldevname; | |
b8dfe498 | 72 | ret = ifname_compare_aligned(indev, info->physindev, info->in_mask); |
1da177e4 | 73 | |
1d93a9cb JE |
74 | if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN)) |
75 | return false; | |
1da177e4 LT |
76 | |
77 | match_outdev: | |
2e4e6a17 | 78 | if (!(info->bitmask & XT_PHYSDEV_OP_OUT)) |
1d93a9cb | 79 | return true; |
1da177e4 LT |
80 | outdev = nf_bridge->physoutdev ? |
81 | nf_bridge->physoutdev->name : nulldevname; | |
b8dfe498 | 82 | ret = ifname_compare_aligned(outdev, info->physoutdev, info->out_mask); |
eacc17fb | 83 | |
4f1c3b7e | 84 | return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT)); |
1da177e4 LT |
85 | } |
86 | ||
b0f38452 | 87 | static int physdev_mt_check(const struct xt_mtchk_param *par) |
1da177e4 | 88 | { |
9b4fce7a | 89 | const struct xt_physdev_info *info = par->matchinfo; |
1da177e4 | 90 | |
4b7fd5d9 PNA |
91 | br_netfilter_enable(); |
92 | ||
2e4e6a17 HW |
93 | if (!(info->bitmask & XT_PHYSDEV_OP_MASK) || |
94 | info->bitmask & ~XT_PHYSDEV_OP_MASK) | |
bd414ee6 | 95 | return -EINVAL; |
2bf540b7 | 96 | if (info->bitmask & XT_PHYSDEV_OP_OUT && |
10ea6ac8 PM |
97 | (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) || |
98 | info->invert & XT_PHYSDEV_OP_BRIDGED) && | |
9b4fce7a JE |
99 | par->hook_mask & ((1 << NF_INET_LOCAL_OUT) | |
100 | (1 << NF_INET_FORWARD) | (1 << NF_INET_POST_ROUTING))) { | |
8bee4bad JE |
101 | pr_info("using --physdev-out in the OUTPUT, FORWARD and " |
102 | "POSTROUTING chains for non-bridged traffic is not " | |
103 | "supported anymore.\n"); | |
9b4fce7a | 104 | if (par->hook_mask & (1 << NF_INET_LOCAL_OUT)) |
bd414ee6 | 105 | return -EINVAL; |
10ea6ac8 | 106 | } |
bd414ee6 | 107 | return 0; |
1da177e4 LT |
108 | } |
109 | ||
ab4f21e6 JE |
110 | static struct xt_match physdev_mt_reg __read_mostly = { |
111 | .name = "physdev", | |
112 | .revision = 0, | |
113 | .family = NFPROTO_UNSPEC, | |
114 | .checkentry = physdev_mt_check, | |
115 | .match = physdev_mt, | |
116 | .matchsize = sizeof(struct xt_physdev_info), | |
117 | .me = THIS_MODULE, | |
1da177e4 LT |
118 | }; |
119 | ||
d3c5ee6d | 120 | static int __init physdev_mt_init(void) |
1da177e4 | 121 | { |
ab4f21e6 | 122 | return xt_register_match(&physdev_mt_reg); |
1da177e4 LT |
123 | } |
124 | ||
d3c5ee6d | 125 | static void __exit physdev_mt_exit(void) |
1da177e4 | 126 | { |
ab4f21e6 | 127 | xt_unregister_match(&physdev_mt_reg); |
1da177e4 LT |
128 | } |
129 | ||
d3c5ee6d JE |
130 | module_init(physdev_mt_init); |
131 | module_exit(physdev_mt_exit); |