blob: ec6ed6fda96c5903d6136fce62f82912dc0701cb [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Harald Welte2e4e6a12006-01-12 13:30:04 -08002/* 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>
Harald Welte2e4e6a12006-01-12 13:30:04 -08006 */
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +01007#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Jeremy Sowden85cfbc22019-09-13 09:13:04 +01008
9#include <linux/if.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080010#include <linux/module.h>
11#include <linux/skbuff.h>
Andrew Mortondeb47c62006-08-15 00:04:56 -070012#include <linux/netfilter_bridge.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080013#include <linux/netfilter/x_tables.h>
Jeremy Sowden85cfbc22019-09-13 09:13:04 +010014#include <uapi/linux/netfilter/xt_physdev.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080015
16MODULE_LICENSE("GPL");
17MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080018MODULE_DESCRIPTION("Xtables: Bridge physical device match");
Harald Welte2e4e6a12006-01-12 13:30:04 -080019MODULE_ALIAS("ipt_physdev");
20MODULE_ALIAS("ip6t_physdev");
21
Eric Dumazeteacc17f2009-02-19 11:17:17 +010022
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070023static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020024physdev_mt(const struct sk_buff *skb, struct xt_action_param *par)
Harald Welte2e4e6a12006-01-12 13:30:04 -080025{
Jan Engelhardtf7108a202008-10-08 11:35:18 +020026 const struct xt_physdev_info *info = par->matchinfo;
Florian Westphala99074a2015-04-02 14:31:42 +020027 const struct net_device *physdev;
Eric Dumazet4f1c3b72009-02-18 19:11:39 +010028 unsigned long ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -080029 const char *indev, *outdev;
Harald Welte2e4e6a12006-01-12 13:30:04 -080030
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. */
Florian Westphalc4b0e772018-12-18 17:15:15 +010034 if (!nf_bridge_info_exists(skb)) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080035 /* Return MATCH if the invert flags of the used options are on */
36 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
37 !(info->invert & XT_PHYSDEV_OP_BRIDGED))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070038 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080039 if ((info->bitmask & XT_PHYSDEV_OP_ISIN) &&
40 !(info->invert & XT_PHYSDEV_OP_ISIN))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070041 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080042 if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) &&
43 !(info->invert & XT_PHYSDEV_OP_ISOUT))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070044 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080045 if ((info->bitmask & XT_PHYSDEV_OP_IN) &&
46 !(info->invert & XT_PHYSDEV_OP_IN))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070047 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080048 if ((info->bitmask & XT_PHYSDEV_OP_OUT) &&
49 !(info->invert & XT_PHYSDEV_OP_OUT))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070050 return false;
51 return true;
Harald Welte2e4e6a12006-01-12 13:30:04 -080052 }
53
Florian Westphala99074a2015-04-02 14:31:42 +020054 physdev = nf_bridge_get_physoutdev(skb);
55 outdev = physdev ? physdev->name : NULL;
56
Harald Welte2e4e6a12006-01-12 13:30:04 -080057 /* This only makes sense in the FORWARD and POSTROUTING chains */
58 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
Florian Westphala99074a2015-04-02 14:31:42 +020059 (!!outdev ^ !(info->invert & XT_PHYSDEV_OP_BRIDGED)))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070060 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080061
Florian Westphala99074a2015-04-02 14:31:42 +020062 physdev = nf_bridge_get_physindev(skb);
63 indev = physdev ? physdev->name : NULL;
64
Harald Welte2e4e6a12006-01-12 13:30:04 -080065 if ((info->bitmask & XT_PHYSDEV_OP_ISIN &&
Florian Westphala99074a2015-04-02 14:31:42 +020066 (!indev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) ||
Harald Welte2e4e6a12006-01-12 13:30:04 -080067 (info->bitmask & XT_PHYSDEV_OP_ISOUT &&
Florian Westphala99074a2015-04-02 14:31:42 +020068 (!outdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT))))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070069 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080070
71 if (!(info->bitmask & XT_PHYSDEV_OP_IN))
72 goto match_outdev;
Harald Welte2e4e6a12006-01-12 13:30:04 -080073
Florian Westphala99074a2015-04-02 14:31:42 +020074 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 }
Harald Welte2e4e6a12006-01-12 13:30:04 -080081
82match_outdev:
83 if (!(info->bitmask & XT_PHYSDEV_OP_OUT))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070084 return true;
Florian Westphala99074a2015-04-02 14:31:42 +020085
86 if (!outdev)
87 return false;
88
Eric Dumazetb8dfe492009-03-25 17:31:52 +010089 ret = ifname_compare_aligned(outdev, info->physoutdev, info->out_mask);
Eric Dumazeteacc17f2009-02-19 11:17:17 +010090
Eric Dumazet4f1c3b72009-02-18 19:11:39 +010091 return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT));
Harald Welte2e4e6a12006-01-12 13:30:04 -080092}
93
Jan Engelhardtb0f38452010-03-19 17:16:42 +010094static int physdev_mt_check(const struct xt_mtchk_param *par)
Harald Welte2e4e6a12006-01-12 13:30:04 -080095{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020096 const struct xt_physdev_info *info = par->matchinfo;
Florian Westphal8e2f3112019-01-11 14:46:15 +010097 static bool brnf_probed __read_mostly;
Pablo Neira Ayuso4b7fd5d2014-10-02 11:13:21 +020098
Harald Welte2e4e6a12006-01-12 13:30:04 -080099 if (!(info->bitmask & XT_PHYSDEV_OP_MASK) ||
100 info->bitmask & ~XT_PHYSDEV_OP_MASK)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100101 return -EINVAL;
Hangbin Liu47c74452016-07-05 20:55:36 +0800102 if (info->bitmask & (XT_PHYSDEV_OP_OUT | XT_PHYSDEV_OP_ISOUT) &&
Patrick McHardy10ea6ac2006-07-24 22:54:55 -0700103 (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) ||
104 info->invert & XT_PHYSDEV_OP_BRIDGED) &&
Todd Seidelmann3cf2f452019-08-21 11:47:53 -0400105 par->hook_mask & (1 << NF_INET_LOCAL_OUT)) {
Florian Westphalb2606642018-02-09 15:52:07 +0100106 pr_info_ratelimited("--physdev-out and --physdev-is-out only supported in the FORWARD and POSTROUTING chains with bridged traffic\n");
Todd Seidelmann3cf2f452019-08-21 11:47:53 -0400107 return -EINVAL;
Patrick McHardy10ea6ac2006-07-24 22:54:55 -0700108 }
Florian Westphal8e2f3112019-01-11 14:46:15 +0100109
110 if (!brnf_probed) {
111 brnf_probed = true;
112 request_module("br_netfilter");
113 }
114
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100115 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800116}
117
Jan Engelhardtab4f21e2008-10-08 11:35:20 +0200118static 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,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800126};
127
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800128static int __init physdev_mt_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800129{
Jan Engelhardtab4f21e2008-10-08 11:35:20 +0200130 return xt_register_match(&physdev_mt_reg);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800131}
132
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800133static void __exit physdev_mt_exit(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800134{
Jan Engelhardtab4f21e2008-10-08 11:35:20 +0200135 xt_unregister_match(&physdev_mt_reg);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800136}
137
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800138module_init(physdev_mt_init);
139module_exit(physdev_mt_exit);