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