blob: f440f57a452fd9650d73f0a13b644383793463f5 [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{
Eric Dumazet4f1c3b72009-02-18 19:11:39 +010028 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Jan Engelhardtf7108a22008-10-08 11:35:18 +020029 const struct xt_physdev_info *info = par->matchinfo;
Eric Dumazet4f1c3b72009-02-18 19:11:39 +010030 unsigned long ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -080031 const char *indev, *outdev;
Jan Engelhardta47362a2007-07-07 22:16:55 -070032 const struct nf_bridge_info *nf_bridge;
Harald Welte2e4e6a12006-01-12 13:30:04 -080033
34 /* Not a bridged IP packet or no info available yet:
35 * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if
36 * the destination device will be a bridge. */
37 if (!(nf_bridge = skb->nf_bridge)) {
38 /* Return MATCH if the invert flags of the used options are on */
39 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
40 !(info->invert & XT_PHYSDEV_OP_BRIDGED))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070041 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080042 if ((info->bitmask & XT_PHYSDEV_OP_ISIN) &&
43 !(info->invert & XT_PHYSDEV_OP_ISIN))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070044 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080045 if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) &&
46 !(info->invert & XT_PHYSDEV_OP_ISOUT))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070047 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080048 if ((info->bitmask & XT_PHYSDEV_OP_IN) &&
49 !(info->invert & XT_PHYSDEV_OP_IN))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070050 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080051 if ((info->bitmask & XT_PHYSDEV_OP_OUT) &&
52 !(info->invert & XT_PHYSDEV_OP_OUT))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070053 return false;
54 return true;
Harald Welte2e4e6a12006-01-12 13:30:04 -080055 }
56
57 /* This only makes sense in the FORWARD and POSTROUTING chains */
58 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
59 (!!(nf_bridge->mask & BRNF_BRIDGED) ^
60 !(info->invert & XT_PHYSDEV_OP_BRIDGED)))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070061 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080062
63 if ((info->bitmask & XT_PHYSDEV_OP_ISIN &&
64 (!nf_bridge->physindev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) ||
65 (info->bitmask & XT_PHYSDEV_OP_ISOUT &&
66 (!nf_bridge->physoutdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT))))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070067 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080068
69 if (!(info->bitmask & XT_PHYSDEV_OP_IN))
70 goto match_outdev;
71 indev = nf_bridge->physindev ? nf_bridge->physindev->name : nulldevname;
Eric Dumazetb8dfe492009-03-25 17:31:52 +010072 ret = ifname_compare_aligned(indev, info->physindev, info->in_mask);
Harald Welte2e4e6a12006-01-12 13:30:04 -080073
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070074 if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN))
75 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080076
77match_outdev:
78 if (!(info->bitmask & XT_PHYSDEV_OP_OUT))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070079 return true;
Harald Welte2e4e6a12006-01-12 13:30:04 -080080 outdev = nf_bridge->physoutdev ?
81 nf_bridge->physoutdev->name : nulldevname;
Eric Dumazetb8dfe492009-03-25 17:31:52 +010082 ret = ifname_compare_aligned(outdev, info->physoutdev, info->out_mask);
Eric Dumazeteacc17f2009-02-19 11:17:17 +010083
Eric Dumazet4f1c3b72009-02-18 19:11:39 +010084 return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT));
Harald Welte2e4e6a12006-01-12 13:30:04 -080085}
86
Jan Engelhardtb0f38452010-03-19 17:16:42 +010087static int physdev_mt_check(const struct xt_mtchk_param *par)
Harald Welte2e4e6a12006-01-12 13:30:04 -080088{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020089 const struct xt_physdev_info *info = par->matchinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -080090
Pablo Neira Ayuso4b7fd5d2014-10-02 11:13:21 +020091 br_netfilter_enable();
92
Harald Welte2e4e6a12006-01-12 13:30:04 -080093 if (!(info->bitmask & XT_PHYSDEV_OP_MASK) ||
94 info->bitmask & ~XT_PHYSDEV_OP_MASK)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010095 return -EINVAL;
Patrick McHardy2bf540b2006-12-13 16:54:25 -080096 if (info->bitmask & XT_PHYSDEV_OP_OUT &&
Patrick McHardy10ea6ac2006-07-24 22:54:55 -070097 (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) ||
98 info->invert & XT_PHYSDEV_OP_BRIDGED) &&
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020099 par->hook_mask & ((1 << NF_INET_LOCAL_OUT) |
100 (1 << NF_INET_FORWARD) | (1 << NF_INET_POST_ROUTING))) {
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +0100101 pr_info("using --physdev-out in the OUTPUT, FORWARD and "
102 "POSTROUTING chains for non-bridged traffic is not "
103 "supported anymore.\n");
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200104 if (par->hook_mask & (1 << NF_INET_LOCAL_OUT))
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100105 return -EINVAL;
Patrick McHardy10ea6ac2006-07-24 22:54:55 -0700106 }
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100107 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800108}
109
Jan Engelhardtab4f21e2008-10-08 11:35:20 +0200110static struct xt_match physdev_mt_reg __read_mostly = {
111 .name = "physdev",
112 .revision = 0,
113 .family = NFPROTO_UNSPEC,
114 .checkentry = physdev_mt_check,
115 .match = physdev_mt,
116 .matchsize = sizeof(struct xt_physdev_info),
117 .me = THIS_MODULE,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800118};
119
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800120static int __init physdev_mt_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800121{
Jan Engelhardtab4f21e2008-10-08 11:35:20 +0200122 return xt_register_match(&physdev_mt_reg);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800123}
124
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800125static void __exit physdev_mt_exit(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800126{
Jan Engelhardtab4f21e2008-10-08 11:35:20 +0200127 xt_unregister_match(&physdev_mt_reg);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800128}
129
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800130module_init(physdev_mt_init);
131module_exit(physdev_mt_exit);