Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1 | /* 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 Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/skbuff.h> |
Andrew Morton | deb47c6 | 2006-08-15 00:04:56 -0700 | [diff] [blame] | 13 | #include <linux/netfilter_bridge.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 14 | #include <linux/netfilter/xt_physdev.h> |
| 15 | #include <linux/netfilter/x_tables.h> |
Pablo Neira Ayuso | 4b7fd5d | 2014-10-02 11:13:21 +0200 | [diff] [blame] | 16 | #include <net/netfilter/br_netfilter.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 17 | |
| 18 | MODULE_LICENSE("GPL"); |
| 19 | MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 20 | MODULE_DESCRIPTION("Xtables: Bridge physical device match"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 21 | MODULE_ALIAS("ipt_physdev"); |
| 22 | MODULE_ALIAS("ip6t_physdev"); |
| 23 | |
Eric Dumazet | eacc17f | 2009-02-19 11:17:17 +0100 | [diff] [blame] | 24 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 25 | static bool |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 26 | physdev_mt(const struct sk_buff *skb, struct xt_action_param *par) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 27 | { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 28 | const struct xt_physdev_info *info = par->matchinfo; |
Florian Westphal | a99074a | 2015-04-02 14:31:42 +0200 | [diff] [blame^] | 29 | const struct net_device *physdev; |
Eric Dumazet | 4f1c3b7 | 2009-02-18 19:11:39 +0100 | [diff] [blame] | 30 | unsigned long ret; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 31 | const char *indev, *outdev; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 32 | |
| 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 Westphal | a99074a | 2015-04-02 14:31:42 +0200 | [diff] [blame^] | 36 | if (!skb->nf_bridge) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 37 | /* 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 Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 40 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 41 | if ((info->bitmask & XT_PHYSDEV_OP_ISIN) && |
| 42 | !(info->invert & XT_PHYSDEV_OP_ISIN)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 43 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 44 | if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) && |
| 45 | !(info->invert & XT_PHYSDEV_OP_ISOUT)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 46 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 47 | if ((info->bitmask & XT_PHYSDEV_OP_IN) && |
| 48 | !(info->invert & XT_PHYSDEV_OP_IN)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 49 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 50 | if ((info->bitmask & XT_PHYSDEV_OP_OUT) && |
| 51 | !(info->invert & XT_PHYSDEV_OP_OUT)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 52 | return false; |
| 53 | return true; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Florian Westphal | a99074a | 2015-04-02 14:31:42 +0200 | [diff] [blame^] | 56 | physdev = nf_bridge_get_physoutdev(skb); |
| 57 | outdev = physdev ? physdev->name : NULL; |
| 58 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 59 | /* This only makes sense in the FORWARD and POSTROUTING chains */ |
| 60 | if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) && |
Florian Westphal | a99074a | 2015-04-02 14:31:42 +0200 | [diff] [blame^] | 61 | (!!outdev ^ !(info->invert & XT_PHYSDEV_OP_BRIDGED))) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 62 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 63 | |
Florian Westphal | a99074a | 2015-04-02 14:31:42 +0200 | [diff] [blame^] | 64 | physdev = nf_bridge_get_physindev(skb); |
| 65 | indev = physdev ? physdev->name : NULL; |
| 66 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 67 | if ((info->bitmask & XT_PHYSDEV_OP_ISIN && |
Florian Westphal | a99074a | 2015-04-02 14:31:42 +0200 | [diff] [blame^] | 68 | (!indev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) || |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 69 | (info->bitmask & XT_PHYSDEV_OP_ISOUT && |
Florian Westphal | a99074a | 2015-04-02 14:31:42 +0200 | [diff] [blame^] | 70 | (!outdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT)))) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 71 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 72 | |
| 73 | if (!(info->bitmask & XT_PHYSDEV_OP_IN)) |
| 74 | goto match_outdev; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 75 | |
Florian Westphal | a99074a | 2015-04-02 14:31:42 +0200 | [diff] [blame^] | 76 | 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 Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 83 | |
| 84 | match_outdev: |
| 85 | if (!(info->bitmask & XT_PHYSDEV_OP_OUT)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 86 | return true; |
Florian Westphal | a99074a | 2015-04-02 14:31:42 +0200 | [diff] [blame^] | 87 | |
| 88 | if (!outdev) |
| 89 | return false; |
| 90 | |
Eric Dumazet | b8dfe49 | 2009-03-25 17:31:52 +0100 | [diff] [blame] | 91 | ret = ifname_compare_aligned(outdev, info->physoutdev, info->out_mask); |
Eric Dumazet | eacc17f | 2009-02-19 11:17:17 +0100 | [diff] [blame] | 92 | |
Eric Dumazet | 4f1c3b7 | 2009-02-18 19:11:39 +0100 | [diff] [blame] | 93 | return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 96 | static int physdev_mt_check(const struct xt_mtchk_param *par) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 97 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 98 | const struct xt_physdev_info *info = par->matchinfo; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 99 | |
Pablo Neira Ayuso | 4b7fd5d | 2014-10-02 11:13:21 +0200 | [diff] [blame] | 100 | br_netfilter_enable(); |
| 101 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 102 | if (!(info->bitmask & XT_PHYSDEV_OP_MASK) || |
| 103 | info->bitmask & ~XT_PHYSDEV_OP_MASK) |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 104 | return -EINVAL; |
Patrick McHardy | 2bf540b | 2006-12-13 16:54:25 -0800 | [diff] [blame] | 105 | if (info->bitmask & XT_PHYSDEV_OP_OUT && |
Patrick McHardy | 10ea6ac | 2006-07-24 22:54:55 -0700 | [diff] [blame] | 106 | (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) || |
| 107 | info->invert & XT_PHYSDEV_OP_BRIDGED) && |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 108 | par->hook_mask & ((1 << NF_INET_LOCAL_OUT) | |
| 109 | (1 << NF_INET_FORWARD) | (1 << NF_INET_POST_ROUTING))) { |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 110 | pr_info("using --physdev-out in the OUTPUT, FORWARD and " |
| 111 | "POSTROUTING chains for non-bridged traffic is not " |
| 112 | "supported anymore.\n"); |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 113 | if (par->hook_mask & (1 << NF_INET_LOCAL_OUT)) |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 114 | return -EINVAL; |
Patrick McHardy | 10ea6ac | 2006-07-24 22:54:55 -0700 | [diff] [blame] | 115 | } |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 116 | return 0; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Jan Engelhardt | ab4f21e | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 119 | static 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, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 129 | static int __init physdev_mt_init(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 130 | { |
Jan Engelhardt | ab4f21e | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 131 | return xt_register_match(&physdev_mt_reg); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 134 | static void __exit physdev_mt_exit(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 135 | { |
Jan Engelhardt | ab4f21e | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 136 | xt_unregister_match(&physdev_mt_reg); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 139 | module_init(physdev_mt_init); |
| 140 | module_exit(physdev_mt_exit); |