| 1 | |
| 2 | |
| 3 | * out device for IP packets coming into contact with a bridge.
|
| 4 |
|
| 5 | |
| 6 |
|
| 7 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
| 8 |
|
| 9 | #include <linux/if.h>
|
| 10 | #include <linux/module.h>
|
| 11 | #include <linux/skbuff.h>
|
| 12 | #include <linux/netfilter_bridge.h>
|
| 13 | #include <linux/netfilter/x_tables.h>
|
| 14 | #include <uapi/linux/netfilter/xt_physdev.h>
|
| 15 |
|
| 16 | MODULE_LICENSE("GPL");
|
| 17 | MODULE_AUTHOR("Bart De Schuymer <[email protected]>");
|
| 18 | MODULE_DESCRIPTION("Xtables: Bridge physical device match");
|
| 19 | MODULE_ALIAS("ipt_physdev");
|
| 20 | MODULE_ALIAS("ip6t_physdev");
|
| 21 |
|
| 22 |
|
| 23 | static bool
|
| 24 | physdev_mt(const struct sk_buff *skb, struct xt_action_param *par)
|
| 25 | {
|
| 26 | const struct xt_physdev_info *info = par->matchinfo;
|
| 27 | const struct net_device *physdev;
|
| 28 | unsigned long ret;
|
| 29 | const char *indev, *outdev;
|
| 30 |
|
| 31 | |
| 32 | * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if
|
| 33 | * the destination device will be a bridge.
|
| 34 | if (!nf_bridge_info_exists(skb)) {
|
| 35 |
|
| 36 | if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
|
| 37 | !(info->invert & XT_PHYSDEV_OP_BRIDGED))
|
| 38 | return false;
|
| 39 | if ((info->bitmask & XT_PHYSDEV_OP_ISIN) &&
|
| 40 | !(info->invert & XT_PHYSDEV_OP_ISIN))
|
| 41 | return false;
|
| 42 | if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) &&
|
| 43 | !(info->invert & XT_PHYSDEV_OP_ISOUT))
|
| 44 | return false;
|
| 45 | if ((info->bitmask & XT_PHYSDEV_OP_IN) &&
|
| 46 | !(info->invert & XT_PHYSDEV_OP_IN))
|
| 47 | return false;
|
| 48 | if ((info->bitmask & XT_PHYSDEV_OP_OUT) &&
|
| 49 | !(info->invert & XT_PHYSDEV_OP_OUT))
|
| 50 | return false;
|
| 51 | return true;
|
| 52 | }
|
| 53 |
|
| 54 | physdev = nf_bridge_get_physoutdev(skb);
|
| 55 | outdev = physdev ? physdev->name : NULL;
|
| 56 |
|
| 57 |
|
| 58 | if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
|
| 59 | (!!outdev ^ !(info->invert & XT_PHYSDEV_OP_BRIDGED)))
|
| 60 | return false;
|
| 61 |
|
| 62 | physdev = nf_bridge_get_physindev(skb, xt_net(par));
|
| 63 | indev = physdev ? physdev->name : NULL;
|
| 64 |
|
| 65 | if ((info->bitmask & XT_PHYSDEV_OP_ISIN &&
|
| 66 | (!indev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) ||
|
| 67 | (info->bitmask & XT_PHYSDEV_OP_ISOUT &&
|
| 68 | (!outdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT))))
|
| 69 | return false;
|
| 70 |
|
| 71 | if (!(info->bitmask & XT_PHYSDEV_OP_IN))
|
| 72 | goto match_outdev;
|
| 73 |
|
| 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 | }
|
| 81 |
|
| 82 | match_outdev:
|
| 83 | if (!(info->bitmask & XT_PHYSDEV_OP_OUT))
|
| 84 | return true;
|
| 85 |
|
| 86 | if (!outdev)
|
| 87 | return false;
|
| 88 |
|
| 89 | ret = ifname_compare_aligned(outdev, info->physoutdev, info->out_mask);
|
| 90 |
|
| 91 | return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT));
|
| 92 | }
|
| 93 |
|
| 94 | static int physdev_mt_check_hooks(const struct xt_mtchk_param *par)
|
| 95 | {
|
| 96 | const struct xt_physdev_info *info = par->matchinfo;
|
| 97 |
|
| 98 | if (info->bitmask & (XT_PHYSDEV_OP_OUT | XT_PHYSDEV_OP_ISOUT) &&
|
| 99 | (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) ||
|
| 100 | info->invert & XT_PHYSDEV_OP_BRIDGED) &&
|
| 101 | par->hook_mask & (1 << NF_INET_LOCAL_OUT)) {
|
| 102 | pr_info_ratelimited("--physdev-out and --physdev-is-out only supported in the FORWARD and POSTROUTING chains with bridged traffic\n");
|
| 103 | return -EINVAL;
|
| 104 | }
|
| 105 |
|
| 106 | return 0;
|
| 107 | }
|
| 108 |
|
| 109 | static int physdev_mt_check(const struct xt_mtchk_param *par)
|
| 110 | {
|
| 111 | const struct xt_physdev_info *info = par->matchinfo;
|
| 112 | static bool brnf_probed __read_mostly;
|
| 113 |
|
| 114 | if (!(info->bitmask & XT_PHYSDEV_OP_MASK) ||
|
| 115 | info->bitmask & ~XT_PHYSDEV_OP_MASK)
|
| 116 | return -EINVAL;
|
| 117 |
|
| 118 | #define X(memb) strnlen(info->memb, sizeof(info->memb)) >= sizeof(info->memb)
|
| 119 | if (info->bitmask & XT_PHYSDEV_OP_IN) {
|
| 120 | if (info->physindev[0] == '\0')
|
| 121 | return -EINVAL;
|
| 122 | if (X(physindev))
|
| 123 | return -ENAMETOOLONG;
|
| 124 | }
|
| 125 |
|
| 126 | if (info->bitmask & XT_PHYSDEV_OP_OUT) {
|
| 127 | if (info->physoutdev[0] == '\0')
|
| 128 | return -EINVAL;
|
| 129 |
|
| 130 | if (X(physoutdev))
|
| 131 | return -ENAMETOOLONG;
|
| 132 | }
|
| 133 | #undef X
|
| 134 |
|
| 135 | if (!brnf_probed) {
|
| 136 | brnf_probed = true;
|
| 137 | request_module("br_netfilter");
|
| 138 | }
|
| 139 |
|
| 140 | return 0;
|
| 141 | }
|
| 142 |
|
| 143 | static struct xt_match physdev_mt_reg[] __read_mostly = {
|
| 144 | {
|
| 145 | .name = "physdev",
|
| 146 | .family = NFPROTO_IPV4,
|
| 147 | .check_hooks = physdev_mt_check_hooks,
|
| 148 | .checkentry = physdev_mt_check,
|
| 149 | .match = physdev_mt,
|
| 150 | .matchsize = sizeof(struct xt_physdev_info),
|
| 151 | .me = THIS_MODULE,
|
| 152 | },
|
| 153 | {
|
| 154 | .name = "physdev",
|
| 155 | .family = NFPROTO_IPV6,
|
| 156 | .check_hooks = physdev_mt_check_hooks,
|
| 157 | .checkentry = physdev_mt_check,
|
| 158 | .match = physdev_mt,
|
| 159 | .matchsize = sizeof(struct xt_physdev_info),
|
| 160 | .me = THIS_MODULE,
|
| 161 | },
|
| 162 | };
|
| 163 |
|
| 164 | static int __init physdev_mt_init(void)
|
| 165 | {
|
| 166 | return xt_register_matches(physdev_mt_reg, ARRAY_SIZE(physdev_mt_reg));
|
| 167 | }
|
| 168 |
|
| 169 | static void __exit physdev_mt_exit(void)
|
| 170 | {
|
| 171 | xt_unregister_matches(physdev_mt_reg, ARRAY_SIZE(physdev_mt_reg));
|
| 172 | }
|
| 173 |
|
| 174 | module_init(physdev_mt_init);
|
| 175 | module_exit(physdev_mt_exit);
|