]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* Kernel module to match the bridge port in and |
3 | * out device for IP packets coming into contact with a bridge. */ | |
4 | ||
5 | /* (C) 2001-2003 Bart De Schuymer <bdschuym@pandora.be> | |
1da177e4 | 6 | */ |
8bee4bad | 7 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
85cfbc25 JS |
8 | |
9 | #include <linux/if.h> | |
1da177e4 LT |
10 | #include <linux/module.h> |
11 | #include <linux/skbuff.h> | |
deb47c66 | 12 | #include <linux/netfilter_bridge.h> |
2e4e6a17 | 13 | #include <linux/netfilter/x_tables.h> |
85cfbc25 | 14 | #include <uapi/linux/netfilter/xt_physdev.h> |
1da177e4 LT |
15 | |
16 | MODULE_LICENSE("GPL"); | |
17 | MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>"); | |
2ae15b64 | 18 | MODULE_DESCRIPTION("Xtables: Bridge physical device match"); |
2e4e6a17 HW |
19 | MODULE_ALIAS("ipt_physdev"); |
20 | MODULE_ALIAS("ip6t_physdev"); | |
1da177e4 | 21 | |
eacc17fb | 22 | |
1d93a9cb | 23 | static bool |
62fc8051 | 24 | physdev_mt(const struct sk_buff *skb, struct xt_action_param *par) |
1da177e4 | 25 | { |
f7108a20 | 26 | const struct xt_physdev_info *info = par->matchinfo; |
a99074ae | 27 | const struct net_device *physdev; |
4f1c3b7e | 28 | unsigned long ret; |
1da177e4 | 29 | const char *indev, *outdev; |
1da177e4 LT |
30 | |
31 | /* Not a bridged IP packet or no info available yet: | |
32 | * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if | |
33 | * the destination device will be a bridge. */ | |
c4b0e771 | 34 | if (!nf_bridge_info_exists(skb)) { |
1da177e4 | 35 | /* Return MATCH if the invert flags of the used options are on */ |
2e4e6a17 HW |
36 | if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) && |
37 | !(info->invert & XT_PHYSDEV_OP_BRIDGED)) | |
1d93a9cb | 38 | return false; |
2e4e6a17 HW |
39 | if ((info->bitmask & XT_PHYSDEV_OP_ISIN) && |
40 | !(info->invert & XT_PHYSDEV_OP_ISIN)) | |
1d93a9cb | 41 | return false; |
2e4e6a17 HW |
42 | if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) && |
43 | !(info->invert & XT_PHYSDEV_OP_ISOUT)) | |
1d93a9cb | 44 | return false; |
2e4e6a17 HW |
45 | if ((info->bitmask & XT_PHYSDEV_OP_IN) && |
46 | !(info->invert & XT_PHYSDEV_OP_IN)) | |
1d93a9cb | 47 | return false; |
2e4e6a17 HW |
48 | if ((info->bitmask & XT_PHYSDEV_OP_OUT) && |
49 | !(info->invert & XT_PHYSDEV_OP_OUT)) | |
1d93a9cb JE |
50 | return false; |
51 | return true; | |
1da177e4 LT |
52 | } |
53 | ||
a99074ae FW |
54 | physdev = nf_bridge_get_physoutdev(skb); |
55 | outdev = physdev ? physdev->name : NULL; | |
56 | ||
1da177e4 | 57 | /* This only makes sense in the FORWARD and POSTROUTING chains */ |
2e4e6a17 | 58 | if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) && |
a99074ae | 59 | (!!outdev ^ !(info->invert & XT_PHYSDEV_OP_BRIDGED))) |
1d93a9cb | 60 | return false; |
1da177e4 | 61 | |
a99074ae FW |
62 | physdev = nf_bridge_get_physindev(skb); |
63 | indev = physdev ? physdev->name : NULL; | |
64 | ||
2e4e6a17 | 65 | if ((info->bitmask & XT_PHYSDEV_OP_ISIN && |
a99074ae | 66 | (!indev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) || |
2e4e6a17 | 67 | (info->bitmask & XT_PHYSDEV_OP_ISOUT && |
a99074ae | 68 | (!outdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT)))) |
1d93a9cb | 69 | return false; |
1da177e4 | 70 | |
2e4e6a17 | 71 | if (!(info->bitmask & XT_PHYSDEV_OP_IN)) |
1da177e4 | 72 | goto match_outdev; |
1da177e4 | 73 | |
a99074ae FW |
74 | if (indev) { |
75 | ret = ifname_compare_aligned(indev, info->physindev, | |
76 | info->in_mask); | |
77 | ||
78 | if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN)) | |
79 | return false; | |
80 | } | |
1da177e4 LT |
81 | |
82 | match_outdev: | |
2e4e6a17 | 83 | if (!(info->bitmask & XT_PHYSDEV_OP_OUT)) |
1d93a9cb | 84 | return true; |
a99074ae FW |
85 | |
86 | if (!outdev) | |
87 | return false; | |
88 | ||
b8dfe498 | 89 | ret = ifname_compare_aligned(outdev, info->physoutdev, info->out_mask); |
eacc17fb | 90 | |
4f1c3b7e | 91 | return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT)); |
1da177e4 LT |
92 | } |
93 | ||
b0f38452 | 94 | static int physdev_mt_check(const struct xt_mtchk_param *par) |
1da177e4 | 95 | { |
9b4fce7a | 96 | const struct xt_physdev_info *info = par->matchinfo; |
8e2f311a | 97 | static bool brnf_probed __read_mostly; |
4b7fd5d9 | 98 | |
2e4e6a17 HW |
99 | if (!(info->bitmask & XT_PHYSDEV_OP_MASK) || |
100 | info->bitmask & ~XT_PHYSDEV_OP_MASK) | |
bd414ee6 | 101 | return -EINVAL; |
47c74456 | 102 | if (info->bitmask & (XT_PHYSDEV_OP_OUT | XT_PHYSDEV_OP_ISOUT) && |
10ea6ac8 PM |
103 | (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) || |
104 | info->invert & XT_PHYSDEV_OP_BRIDGED) && | |
3cf2f450 | 105 | par->hook_mask & (1 << NF_INET_LOCAL_OUT)) { |
b2606644 | 106 | pr_info_ratelimited("--physdev-out and --physdev-is-out only supported in the FORWARD and POSTROUTING chains with bridged traffic\n"); |
3cf2f450 | 107 | return -EINVAL; |
10ea6ac8 | 108 | } |
8e2f311a FW |
109 | |
110 | if (!brnf_probed) { | |
111 | brnf_probed = true; | |
112 | request_module("br_netfilter"); | |
113 | } | |
114 | ||
bd414ee6 | 115 | return 0; |
1da177e4 LT |
116 | } |
117 | ||
ab4f21e6 JE |
118 | static struct xt_match physdev_mt_reg __read_mostly = { |
119 | .name = "physdev", | |
120 | .revision = 0, | |
121 | .family = NFPROTO_UNSPEC, | |
122 | .checkentry = physdev_mt_check, | |
123 | .match = physdev_mt, | |
124 | .matchsize = sizeof(struct xt_physdev_info), | |
125 | .me = THIS_MODULE, | |
1da177e4 LT |
126 | }; |
127 | ||
d3c5ee6d | 128 | static int __init physdev_mt_init(void) |
1da177e4 | 129 | { |
ab4f21e6 | 130 | return xt_register_match(&physdev_mt_reg); |
1da177e4 LT |
131 | } |
132 | ||
d3c5ee6d | 133 | static void __exit physdev_mt_exit(void) |
1da177e4 | 134 | { |
ab4f21e6 | 135 | xt_unregister_match(&physdev_mt_reg); |
1da177e4 LT |
136 | } |
137 | ||
d3c5ee6d JE |
138 | module_init(physdev_mt_init); |
139 | module_exit(physdev_mt_exit); |