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 | */ |
| 10 | |
| 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> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 16 | |
| 17 | MODULE_LICENSE("GPL"); |
| 18 | MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 19 | MODULE_DESCRIPTION("Xtables: Bridge physical device match"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 20 | MODULE_ALIAS("ipt_physdev"); |
| 21 | MODULE_ALIAS("ip6t_physdev"); |
| 22 | |
Eric Dumazet | eacc17f | 2009-02-19 11:17:17 +0100 | [diff] [blame^] | 23 | static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask) |
| 24 | { |
| 25 | const unsigned long *a = (const unsigned long *)_a; |
| 26 | const unsigned long *b = (const unsigned long *)_b; |
| 27 | const unsigned long *mask = (const unsigned long *)_mask; |
| 28 | unsigned long ret; |
| 29 | |
| 30 | ret = (a[0] ^ b[0]) & mask[0]; |
| 31 | if (IFNAMSIZ > sizeof(unsigned long)) |
| 32 | ret |= (a[1] ^ b[1]) & mask[1]; |
| 33 | if (IFNAMSIZ > 2 * sizeof(unsigned long)) |
| 34 | ret |= (a[2] ^ b[2]) & mask[2]; |
| 35 | if (IFNAMSIZ > 3 * sizeof(unsigned long)) |
| 36 | ret |= (a[3] ^ b[3]) & mask[3]; |
| 37 | BUILD_BUG_ON(IFNAMSIZ > 4 * sizeof(unsigned long)); |
| 38 | return ret; |
| 39 | } |
| 40 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 41 | static bool |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 42 | physdev_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 43 | { |
Eric Dumazet | 4f1c3b7 | 2009-02-18 19:11:39 +0100 | [diff] [blame] | 44 | static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long)))); |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 45 | const struct xt_physdev_info *info = par->matchinfo; |
Eric Dumazet | 4f1c3b7 | 2009-02-18 19:11:39 +0100 | [diff] [blame] | 46 | unsigned long ret; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 47 | const char *indev, *outdev; |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 48 | const struct nf_bridge_info *nf_bridge; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 49 | |
| 50 | /* Not a bridged IP packet or no info available yet: |
| 51 | * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if |
| 52 | * the destination device will be a bridge. */ |
| 53 | if (!(nf_bridge = skb->nf_bridge)) { |
| 54 | /* Return MATCH if the invert flags of the used options are on */ |
| 55 | if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) && |
| 56 | !(info->invert & XT_PHYSDEV_OP_BRIDGED)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 57 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 58 | if ((info->bitmask & XT_PHYSDEV_OP_ISIN) && |
| 59 | !(info->invert & XT_PHYSDEV_OP_ISIN)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 60 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 61 | if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) && |
| 62 | !(info->invert & XT_PHYSDEV_OP_ISOUT)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 63 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 64 | if ((info->bitmask & XT_PHYSDEV_OP_IN) && |
| 65 | !(info->invert & XT_PHYSDEV_OP_IN)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 66 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 67 | if ((info->bitmask & XT_PHYSDEV_OP_OUT) && |
| 68 | !(info->invert & XT_PHYSDEV_OP_OUT)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 69 | return false; |
| 70 | return true; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* This only makes sense in the FORWARD and POSTROUTING chains */ |
| 74 | if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) && |
| 75 | (!!(nf_bridge->mask & BRNF_BRIDGED) ^ |
| 76 | !(info->invert & XT_PHYSDEV_OP_BRIDGED))) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 77 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 78 | |
| 79 | if ((info->bitmask & XT_PHYSDEV_OP_ISIN && |
| 80 | (!nf_bridge->physindev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) || |
| 81 | (info->bitmask & XT_PHYSDEV_OP_ISOUT && |
| 82 | (!nf_bridge->physoutdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT)))) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 83 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 84 | |
| 85 | if (!(info->bitmask & XT_PHYSDEV_OP_IN)) |
| 86 | goto match_outdev; |
| 87 | indev = nf_bridge->physindev ? nf_bridge->physindev->name : nulldevname; |
Eric Dumazet | eacc17f | 2009-02-19 11:17:17 +0100 | [diff] [blame^] | 88 | ret = ifname_compare(indev, info->physindev, info->in_mask); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 89 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 90 | if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN)) |
| 91 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 92 | |
| 93 | match_outdev: |
| 94 | if (!(info->bitmask & XT_PHYSDEV_OP_OUT)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 95 | return true; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 96 | outdev = nf_bridge->physoutdev ? |
| 97 | nf_bridge->physoutdev->name : nulldevname; |
Eric Dumazet | eacc17f | 2009-02-19 11:17:17 +0100 | [diff] [blame^] | 98 | ret = ifname_compare(outdev, info->physoutdev, info->out_mask); |
| 99 | |
Eric Dumazet | 4f1c3b7 | 2009-02-18 19:11:39 +0100 | [diff] [blame] | 100 | return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 103 | static bool physdev_mt_check(const struct xt_mtchk_param *par) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 104 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 105 | const struct xt_physdev_info *info = par->matchinfo; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 106 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 107 | if (!(info->bitmask & XT_PHYSDEV_OP_MASK) || |
| 108 | info->bitmask & ~XT_PHYSDEV_OP_MASK) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 109 | return false; |
Patrick McHardy | 2bf540b | 2006-12-13 16:54:25 -0800 | [diff] [blame] | 110 | if (info->bitmask & XT_PHYSDEV_OP_OUT && |
Patrick McHardy | 10ea6ac | 2006-07-24 22:54:55 -0700 | [diff] [blame] | 111 | (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) || |
| 112 | info->invert & XT_PHYSDEV_OP_BRIDGED) && |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 113 | par->hook_mask & ((1 << NF_INET_LOCAL_OUT) | |
| 114 | (1 << NF_INET_FORWARD) | (1 << NF_INET_POST_ROUTING))) { |
Patrick McHardy | 10ea6ac | 2006-07-24 22:54:55 -0700 | [diff] [blame] | 115 | printk(KERN_WARNING "physdev match: using --physdev-out in the " |
| 116 | "OUTPUT, FORWARD and POSTROUTING chains for non-bridged " |
Patrick McHardy | 2bf540b | 2006-12-13 16:54:25 -0800 | [diff] [blame] | 117 | "traffic is not supported anymore.\n"); |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 118 | if (par->hook_mask & (1 << NF_INET_LOCAL_OUT)) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 119 | return false; |
Patrick McHardy | 10ea6ac | 2006-07-24 22:54:55 -0700 | [diff] [blame] | 120 | } |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 121 | return true; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Jan Engelhardt | ab4f21e | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 124 | static 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 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 int __init physdev_mt_init(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 | return xt_register_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 | static void __exit physdev_mt_exit(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 140 | { |
Jan Engelhardt | ab4f21e | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 141 | xt_unregister_match(&physdev_mt_reg); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 144 | module_init(physdev_mt_init); |
| 145 | module_exit(physdev_mt_exit); |