blob: ec247d8370e8d9e9cd72ac0c889cf3d17479696d [file] [log] [blame]
Harald Welte2e4e6a12006-01-12 13:30:04 -08001/* Kernel module to match the bridge port in and
2 * out device for IP packets coming into contact with a bridge. */
3
4/* (C) 2001-2003 Bart De Schuymer <bdschuym@pandora.be>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +010010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Harald Welte2e4e6a12006-01-12 13:30:04 -080011#include <linux/module.h>
12#include <linux/skbuff.h>
Andrew Mortondeb47c62006-08-15 00:04:56 -070013#include <linux/netfilter_bridge.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080014#include <linux/netfilter/xt_physdev.h>
15#include <linux/netfilter/x_tables.h>
Pablo Neira Ayuso4b7fd5d2014-10-02 11:13:21 +020016#include <net/netfilter/br_netfilter.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080017
18MODULE_LICENSE("GPL");
19MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080020MODULE_DESCRIPTION("Xtables: Bridge physical device match");
Harald Welte2e4e6a12006-01-12 13:30:04 -080021MODULE_ALIAS("ipt_physdev");
22MODULE_ALIAS("ip6t_physdev");
23
Eric Dumazeteacc17f2009-02-19 11:17:17 +010024
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070025static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020026physdev_mt(const struct sk_buff *skb, struct xt_action_param *par)
Harald Welte2e4e6a12006-01-12 13:30:04 -080027{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020028 const struct xt_physdev_info *info = par->matchinfo;
Florian Westphala99074a2015-04-02 14:31:42 +020029 const struct net_device *physdev;
Eric Dumazet4f1c3b72009-02-18 19:11:39 +010030 unsigned long ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -080031 const char *indev, *outdev;
Harald Welte2e4e6a12006-01-12 13:30:04 -080032
33 /* Not a bridged IP packet or no info available yet:
34 * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if
35 * the destination device will be a bridge. */
Florian Westphala99074a2015-04-02 14:31:42 +020036 if (!skb->nf_bridge) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080037 /* Return MATCH if the invert flags of the used options are on */
38 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
39 !(info->invert & XT_PHYSDEV_OP_BRIDGED))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070040 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080041 if ((info->bitmask & XT_PHYSDEV_OP_ISIN) &&
42 !(info->invert & XT_PHYSDEV_OP_ISIN))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070043 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080044 if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) &&
45 !(info->invert & XT_PHYSDEV_OP_ISOUT))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070046 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080047 if ((info->bitmask & XT_PHYSDEV_OP_IN) &&
48 !(info->invert & XT_PHYSDEV_OP_IN))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070049 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080050 if ((info->bitmask & XT_PHYSDEV_OP_OUT) &&
51 !(info->invert & XT_PHYSDEV_OP_OUT))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070052 return false;
53 return true;
Harald Welte2e4e6a12006-01-12 13:30:04 -080054 }
55
Florian Westphala99074a2015-04-02 14:31:42 +020056 physdev = nf_bridge_get_physoutdev(skb);
57 outdev = physdev ? physdev->name : NULL;
58
Harald Welte2e4e6a12006-01-12 13:30:04 -080059 /* This only makes sense in the FORWARD and POSTROUTING chains */
60 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
Florian Westphala99074a2015-04-02 14:31:42 +020061 (!!outdev ^ !(info->invert & XT_PHYSDEV_OP_BRIDGED)))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070062 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080063
Florian Westphala99074a2015-04-02 14:31:42 +020064 physdev = nf_bridge_get_physindev(skb);
65 indev = physdev ? physdev->name : NULL;
66
Harald Welte2e4e6a12006-01-12 13:30:04 -080067 if ((info->bitmask & XT_PHYSDEV_OP_ISIN &&
Florian Westphala99074a2015-04-02 14:31:42 +020068 (!indev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) ||
Harald Welte2e4e6a12006-01-12 13:30:04 -080069 (info->bitmask & XT_PHYSDEV_OP_ISOUT &&
Florian Westphala99074a2015-04-02 14:31:42 +020070 (!outdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT))))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070071 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080072
73 if (!(info->bitmask & XT_PHYSDEV_OP_IN))
74 goto match_outdev;
Harald Welte2e4e6a12006-01-12 13:30:04 -080075
Florian Westphala99074a2015-04-02 14:31:42 +020076 if (indev) {
77 ret = ifname_compare_aligned(indev, info->physindev,
78 info->in_mask);
79
80 if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN))
81 return false;
82 }
Harald Welte2e4e6a12006-01-12 13:30:04 -080083
84match_outdev:
85 if (!(info->bitmask & XT_PHYSDEV_OP_OUT))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070086 return true;
Florian Westphala99074a2015-04-02 14:31:42 +020087
88 if (!outdev)
89 return false;
90
Eric Dumazetb8dfe492009-03-25 17:31:52 +010091 ret = ifname_compare_aligned(outdev, info->physoutdev, info->out_mask);
Eric Dumazeteacc17f2009-02-19 11:17:17 +010092
Eric Dumazet4f1c3b72009-02-18 19:11:39 +010093 return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT));
Harald Welte2e4e6a12006-01-12 13:30:04 -080094}
95
Jan Engelhardtb0f38452010-03-19 17:16:42 +010096static int physdev_mt_check(const struct xt_mtchk_param *par)
Harald Welte2e4e6a12006-01-12 13:30:04 -080097{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020098 const struct xt_physdev_info *info = par->matchinfo;
Florian Westphal2ae06da2019-01-11 14:46:15 +010099 static bool brnf_probed __read_mostly;
Pablo Neira Ayuso4b7fd5d2014-10-02 11:13:21 +0200100
Harald Welte2e4e6a12006-01-12 13:30:04 -0800101 if (!(info->bitmask & XT_PHYSDEV_OP_MASK) ||
102 info->bitmask & ~XT_PHYSDEV_OP_MASK)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100103 return -EINVAL;
Hangbin Liu47c74452016-07-05 20:55:36 +0800104 if (info->bitmask & (XT_PHYSDEV_OP_OUT | XT_PHYSDEV_OP_ISOUT) &&
Patrick McHardy10ea6ac2006-07-24 22:54:55 -0700105 (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) ||
106 info->invert & XT_PHYSDEV_OP_BRIDGED) &&
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200107 par->hook_mask & ((1 << NF_INET_LOCAL_OUT) |
108 (1 << NF_INET_FORWARD) | (1 << NF_INET_POST_ROUTING))) {
Hangbin Liuceee4092016-07-25 15:24:43 +0800109 pr_info("using --physdev-out and --physdev-is-out are only "
110 "supported in the FORWARD and POSTROUTING chains with "
Hangbin Liu47c74452016-07-05 20:55:36 +0800111 "bridged traffic.\n");
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200112 if (par->hook_mask & (1 << NF_INET_LOCAL_OUT))
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100113 return -EINVAL;
Patrick McHardy10ea6ac2006-07-24 22:54:55 -0700114 }
Florian Westphal2ae06da2019-01-11 14:46:15 +0100115
116 if (!brnf_probed) {
117 brnf_probed = true;
118 request_module("br_netfilter");
119 }
120
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100121 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800122}
123
Jan Engelhardtab4f21e2008-10-08 11:35:20 +0200124static struct xt_match physdev_mt_reg __read_mostly = {
125 .name = "physdev",
126 .revision = 0,
127 .family = NFPROTO_UNSPEC,
128 .checkentry = physdev_mt_check,
129 .match = physdev_mt,
130 .matchsize = sizeof(struct xt_physdev_info),
131 .me = THIS_MODULE,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800132};
133
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800134static int __init physdev_mt_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800135{
Jan Engelhardtab4f21e2008-10-08 11:35:20 +0200136 return xt_register_match(&physdev_mt_reg);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800137}
138
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800139static void __exit physdev_mt_exit(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800140{
Jan Engelhardtab4f21e2008-10-08 11:35:20 +0200141 xt_unregister_match(&physdev_mt_reg);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800142}
143
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800144module_init(physdev_mt_init);
145module_exit(physdev_mt_exit);