Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Kernel module to match the bridge port in and
0003  * out device for IP packets coming into contact with a bridge. */
0004 
0005 /* (C) 2001-2003 Bart De Schuymer <bdschuym@pandora.be>
0006  */
0007 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0008 
0009 #include <linux/if.h>
0010 #include <linux/module.h>
0011 #include <linux/skbuff.h>
0012 #include <linux/netfilter_bridge.h>
0013 #include <linux/netfilter/x_tables.h>
0014 #include <uapi/linux/netfilter/xt_physdev.h>
0015 
0016 MODULE_LICENSE("GPL");
0017 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
0018 MODULE_DESCRIPTION("Xtables: Bridge physical device match");
0019 MODULE_ALIAS("ipt_physdev");
0020 MODULE_ALIAS("ip6t_physdev");
0021 
0022 
0023 static bool
0024 physdev_mt(const struct sk_buff *skb, struct xt_action_param *par)
0025 {
0026     const struct xt_physdev_info *info = par->matchinfo;
0027     const struct net_device *physdev;
0028     unsigned long ret;
0029     const char *indev, *outdev;
0030 
0031     /* Not a bridged IP packet or no info available yet:
0032      * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if
0033      * the destination device will be a bridge. */
0034     if (!nf_bridge_info_exists(skb)) {
0035         /* Return MATCH if the invert flags of the used options are on */
0036         if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
0037             !(info->invert & XT_PHYSDEV_OP_BRIDGED))
0038             return false;
0039         if ((info->bitmask & XT_PHYSDEV_OP_ISIN) &&
0040             !(info->invert & XT_PHYSDEV_OP_ISIN))
0041             return false;
0042         if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) &&
0043             !(info->invert & XT_PHYSDEV_OP_ISOUT))
0044             return false;
0045         if ((info->bitmask & XT_PHYSDEV_OP_IN) &&
0046             !(info->invert & XT_PHYSDEV_OP_IN))
0047             return false;
0048         if ((info->bitmask & XT_PHYSDEV_OP_OUT) &&
0049             !(info->invert & XT_PHYSDEV_OP_OUT))
0050             return false;
0051         return true;
0052     }
0053 
0054     physdev = nf_bridge_get_physoutdev(skb);
0055     outdev = physdev ? physdev->name : NULL;
0056 
0057     /* This only makes sense in the FORWARD and POSTROUTING chains */
0058     if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
0059         (!!outdev ^ !(info->invert & XT_PHYSDEV_OP_BRIDGED)))
0060         return false;
0061 
0062     physdev = nf_bridge_get_physindev(skb);
0063     indev = physdev ? physdev->name : NULL;
0064 
0065     if ((info->bitmask & XT_PHYSDEV_OP_ISIN &&
0066         (!indev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) ||
0067         (info->bitmask & XT_PHYSDEV_OP_ISOUT &&
0068         (!outdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT))))
0069         return false;
0070 
0071     if (!(info->bitmask & XT_PHYSDEV_OP_IN))
0072         goto match_outdev;
0073 
0074     if (indev) {
0075         ret = ifname_compare_aligned(indev, info->physindev,
0076                          info->in_mask);
0077 
0078         if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN))
0079             return false;
0080     }
0081 
0082 match_outdev:
0083     if (!(info->bitmask & XT_PHYSDEV_OP_OUT))
0084         return true;
0085 
0086     if (!outdev)
0087         return false;
0088 
0089     ret = ifname_compare_aligned(outdev, info->physoutdev, info->out_mask);
0090 
0091     return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT));
0092 }
0093 
0094 static int physdev_mt_check(const struct xt_mtchk_param *par)
0095 {
0096     const struct xt_physdev_info *info = par->matchinfo;
0097     static bool brnf_probed __read_mostly;
0098 
0099     if (!(info->bitmask & XT_PHYSDEV_OP_MASK) ||
0100         info->bitmask & ~XT_PHYSDEV_OP_MASK)
0101         return -EINVAL;
0102     if (info->bitmask & (XT_PHYSDEV_OP_OUT | XT_PHYSDEV_OP_ISOUT) &&
0103         (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) ||
0104          info->invert & XT_PHYSDEV_OP_BRIDGED) &&
0105         par->hook_mask & (1 << NF_INET_LOCAL_OUT)) {
0106         pr_info_ratelimited("--physdev-out and --physdev-is-out only supported in the FORWARD and POSTROUTING chains with bridged traffic\n");
0107         return -EINVAL;
0108     }
0109 
0110     if (!brnf_probed) {
0111         brnf_probed = true;
0112         request_module("br_netfilter");
0113     }
0114 
0115     return 0;
0116 }
0117 
0118 static struct xt_match physdev_mt_reg __read_mostly = {
0119     .name       = "physdev",
0120     .revision   = 0,
0121     .family     = NFPROTO_UNSPEC,
0122     .checkentry = physdev_mt_check,
0123     .match      = physdev_mt,
0124     .matchsize  = sizeof(struct xt_physdev_info),
0125     .me         = THIS_MODULE,
0126 };
0127 
0128 static int __init physdev_mt_init(void)
0129 {
0130     return xt_register_match(&physdev_mt_reg);
0131 }
0132 
0133 static void __exit physdev_mt_exit(void)
0134 {
0135     xt_unregister_match(&physdev_mt_reg);
0136 }
0137 
0138 module_init(physdev_mt_init);
0139 module_exit(physdev_mt_exit);